通过关键词采集文章采集api( 利用Python爬虫采集微博的视频数据requests开发环境版)
优采云 发布时间: 2021-12-09 00:28利用Python爬虫采集微博的视频数据requests开发环境版)
Python爬虫采集微博视频数据
更新时间:2021年12月3日16:00:42 作者:松鼠爱吃饼干
本文文章主要介绍Python爬虫的使用采集微博视频资料,文中有非常详细的代码示例,对学习python的朋友很有帮助,有需要的朋友可以参考
内容
前言
随时随地发现新事物!微博带你领略世间每一个精彩瞬间,了解每一个幕后故事。分享你想表达的,让全世界都能听到你的声音!今天我们用python去采集看微博的好视频!
没错,今天的目标是微博数据采集,爬的就是那些美少女视频
知识点
要求
打印
开发环境
版本:python 3.8
-编辑:pycharm 2021.2
履带原理
功能:批量获取互联网数据(文字、图片、音频、视频)
本质:一次又一次的请求和响应
案例实现
1. 导入需要的模块
import requests
import pprint
2. 找到目标网址
打开开发者工具,选择Fetch/XHR,选择数据所在的标签,找到目标所在的url
3. 发送网络请求
headers = {
'cookie': '',
'referer': 'https://weibo.com/tv/channel/4379160563414111/editor',
'user-agent': '',
}
data = {
'data': '{"Component_Channel_Editor":{"cid":"4379160563414111","count":9}}'
}
url = 'https://www.weibo.com/tv/api/component?page=/tv/channel/4379160563414111/editor'
json_data = requests.post(url=url, headers=headers, data=data).json()
4. 获取数据
json_data_2 = requests.post(url=url_1, headers=headers, data=data_1).json()
5. 过滤数据
dict_urls = json_data_2['data']['Component_Play_Playinfo']['urls']
video_url = "https:" + dict_urls[list(dict_urls.keys())[0]]
print(title + "\t" + video_url)
6. 保存数据
video_data = requests.get(video_url).content
with open(f'video\\{title}.mp4', mode='wb') as f:
f.write(video_data)
print(title, "爬取成功................")
完整代码
import requests
import pprint
headers = {
'cookie': '添加自己的',
'referer': 'https://weibo.com/tv/channel/4379160563414111/editor',
'user-agent': '',
}
data = {
'data': '{"Component_Channel_Editor":{"cid":"4379160563414111","count":9}}'
}
url = 'https://www.weibo.com/tv/api/component?page=/tv/channel/4379160563414111/editor'
json_data = requests.post(url=url, headers=headers, data=data).json()
print(json_data)
ccs_list = json_data['data']['Component_Channel_Editor']['list']
next_cursor = json_data['data']['Component_Channel_Editor']['next_cursor']
for ccs in ccs_list:
oid = ccs['oid']
title = ccs['title']
data_1 = {
'data': '{"Component_Play_Playinfo":{"oid":"' + oid + '"}}'
}
url_1 = 'https://weibo.com/tv/api/component?page=/tv/show/' + oid
json_data_2 = requests.post(url=url_1, headers=headers, data=data_1).json()
dict_urls = json_data_2['data']['Component_Play_Playinfo']['urls']
video_url = "https:" + dict_urls[list(dict_urls.keys())[0]]
print(title + "\t" + video_url)
video_data = requests.get(video_url).content
with open(f'video\\{title}.mp4', mode='wb') as f:
f.write(video_data)
print(title, "爬取成功................")
以上是Python爬虫采集微博视频资料的详细内容。更多Python采集视频资料请关注Script Home的其他相关文章!