15行Python代碼能干嘛?能寫一個抖音網頁版的簡易爬蟲(附源碼)


Python爬蟲、數據分析、網站開發等案例教程視頻免費在線觀看

https://space.bilibili.com/523606542 

 

Python學習交流群:1039649593

前言

相信還有很多人不知道,2021年6月21日,抖音網頁版上線了

 

但是呢,APP版本的,可以下載視頻,保存本地。而網頁版的短視頻,是沒有下載的選線的,所以只能自己寫個爬蟲爬取下來。

不過,你也可以在手機上打開抖音APP,查找到視頻以后在手機的上下載

今天來分享給大家一個只有不到20行代碼的簡單爬蟲,能把你喜歡的抖音視頻一個個的爬取下來
完整代碼

import requests
import re

word = input('請輸入鏈接: ')
# url = 'https://www.douyin.com/video/6967296943450066214?previous_page=main_page'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=word, headers=headers)
html_data = re.findall('src(.*?)vr%3D%2', response.text)[1]
dem = requests.utils.unquote(html_data)
video_url = html_data.replace('%2F', '/').replace('%22%3A%22', 'https:').replace('%3F', '?').replace('%26', '&')
title = re.findall('<title data-react-helmet="true"> (.*?)</title>', response.text)[0]
video_content = requests.get(url=video_url).content
with open(title + '.mp4', mode='wb') as f:
    f.write(video_content)
    print(title, '下載完成')

 

運行結果如下

 
 
有問題可以加群直接找老師一對一解答喲


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM