您好,登錄后才能下訂單哦!
這篇文章主要介紹了Python爬蟲如何采集微博視頻數據,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
requests
pprint
版 本:python 3.8
-編輯器:pycharm 2021.2
作用:批量獲取互聯網數據(文本, 圖片, 音頻, 視頻)
本質:一次次的請求與響應
1. 導入所需模塊
import requests import pprint
2. 找到目標網址
打開開發者工具,選中Fetch/XHR,選中數據所在的標簽,找到目標所在url
https://www.weibo.com/tv/api/component?page=/tv/channel/4379160563414111/editor
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爬蟲如何采集微博視頻數據”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。