Python爬取抖音短視頻(無水印版)

1. 使用更簡單的方法

點(diǎn)到preview選項(xiàng)卡,點(diǎn)擊video->download_addr->url_list

發(fā)現(xiàn)這下面跟著的兩個(gè)網(wǎng)址正好是視頻的網(wǎng)址(根本不需要構(gòu)造,只是這個(gè)網(wǎng)址藏的有點(diǎn)深,需要非常耐心的尋找),打開網(wǎng)址查看:

提取到這個(gè)網(wǎng)址的時(shí)候我以為已經(jīng)結(jié)束了,但是一個(gè)大佬@金亭玉立給我發(fā)來了一篇文章提醒了我一些沒有想到的東西,在此表示感謝!
我們接著往下看,在下面的play_addr下面也有一個(gè)鏈接,這個(gè)鏈接下面的視頻就是無水印版本的視頻



ok,現(xiàn)在我們只要想辦法提取到它就大功告成了
我這次使用的方法中用到了jsonpath模塊,直接pip下載就可以了:
pip?install?jsonpath

2. 代碼
import requestsimport jsonimport jsonpathclass Douyin:def page_num(self,max_cursor):#隨機(jī)碼random_field = '00nvcRAUjgJQBMjqpgesfdNJ72&dytk=4a01c95562f1f10264fb14086512f919'#網(wǎng)址的主體url = 'https://www.iesdouyin.com/web/api/v2/aweme/post/?sec_uid=MS4wLjABAAAAU7Bwg8WznVaafqWLyLUwcVUf9LgrKGYmctJ3n5SwlOA&count=21&max_cursor=' + str(max_cursor) + '&aid=1128&_signature=' + random_field#請求頭headers = {'user-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36',}response = requests.get(url,headers=headers).text#轉(zhuǎn)換成json數(shù)據(jù)resp = json.loads(response)#提取到max_cursormax_cursor = resp['max_cursor']#遍歷for data in resp['aweme_list']:# 視頻簡介video_title = data['desc']#使用jsonpath語法提取paly_addrvideo_url = jsonpath.jsonpath(data,'$..paly_addr')for a in video_url:#提取出來第一個(gè)鏈接地址video_realurl = a['url_list'][1]# 請求視頻video = requests.get(video_realurl, headers=headers).contentwith open('t/' + video_title, 'wb') as f:print('正在下載:', video_title)f.write(video)#判斷停止構(gòu)造網(wǎng)址的條件if max_cursor==0:return 1else:douyin.page_num(max_cursor)if __name__ == '__main__':douyin = Douyin()?????douyin.page_num(max_cursor=0)
3. 優(yōu)點(diǎn)
這個(gè)方法的優(yōu)點(diǎn)是可以省去很大一部分的分析網(wǎng)址的步驟,而且沒有調(diào)用到webdriver(可以不限制瀏覽器),速度也會有顯著提升,得到的視頻也是無水印的
4. 不足
還是沒有解決隨機(jī)生成字符串的問題,操作比較麻煩
原文鏈接:https://www.cnblogs.com/cherish-hao/p/12828027.html
文章轉(zhuǎn)載:Python編程學(xué)習(xí)圈
(版權(quán)歸原作者所有,侵刪)

點(diǎn)擊下方“閱讀原文”查看更多
評論
圖片
表情
