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