[前言]
嗨嘍,大家好,這里是魔王
[本次內容]:
Python批量下載某視頻
[內容介紹]:
短視頻平臺相信大家在家時都刷過,那么你有沒有碰到過你喜歡的作者得視頻突然就沒了,不能看了呢?
為了防止此類事情得發生,這次我來教大家把你喜歡得作者視頻批量下載到本地慢慢看

[知識點]:
動態資料抓包 動態頁面分析 requests攜帶引數發送請求 json資料決議
[第三方庫]:
requests >>> pip install requests
[開發環境]:
- 版 本: python 3.8
- 編輯器:pycharm 2021.2
沒有環境的同學可以找我要安裝教程, 我也錄制了一套安裝基礎環境的教程, 也可以免費找我要哦~
案例實作步驟:
一. 資料來源分析 (只有當你找到資料來源的時候, 才能通過代碼實作)
1. 確定需求 (要爬取的內容是什么?)
爬取用戶下對應的視頻 保存mp4
2. 通過開發者工具進行抓包分析 分析資料從哪里來的(找出真正的資料來源)?
動態加載頁面 開發者工具抓資料包
https://www.kuaishou.com/graphql
做開發的時候 一般來說 開發人員 統一全部用谷歌

二. 代碼實作程序
1. 找到目標網址
2. 發送請求
1.get post
3. 決議資料 (獲取視頻地址 視頻標題)
4. 發送請求 請求每個視頻地址
5. 保存視頻
匯入模塊
import requests # 發送網路請求
import json
一. 找到目標網址
1. 打開一個作者視頻頁面
https://www.kuaishou.com/profile/3xv78fxycm35nn4
代碼
url = 'https://www.kuaishou.com/graphql'
二. 發送請求 訪問網頁 get post
2.打開開發者工具重繪網頁
- 右鍵點擊檢查或者F12打開
- 選擇network然后重繪網頁

3.隨便點擊一個視頻
- 注意圖上的注釋

- 點擊搜到的內容

- 依次展開去查看,找到我們需要的視頻地址

4.確定url地址,請求方式,請求引數,請求頭引數
- 確定url地址,請求方式

- 請求頭引數

- 請求引數

代碼
# 統一替換
# 1.選中要替換的內容
# 2.按住Ctrl+R 注: 點亮星號* / 2021版本一下 點亮Regex
# 3.在第一個框里面輸入(.*?): (.*)
# 4.在第二個框里面輸入'$1': '$2',
# 5.點擊REPLACE ALL
headers = {
# content-type: 代表json型別
'content-type': 'application/json',
'Cookie': 'kpf=PC_WEB; kpn=KUAISHOU_VISION; clientid=3; did=web_9d70ec30050b94472cb5b0b56650bfef; client_key=65890b29; userId=270932146; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABw0yKIfhs_Wdd_A9LYQJSj8fYJgP3h5CfohTpJHme6amEhXivejDIgyNUt0axPa-FAqOns91249zR0VNE4HUyxoEcUv96ZI0hstJJ0rIbUTcIzrhZc4TIkQoUbvo8t-Jpi0JfzHomFMTzpkAWUguRFK4MWdpgsR2au4lRrLBS3fsaHdD-n1Q3U9K8MgB5NzggjhCgJkW-11DZiCN7XnmPXRoSTdCMiCqspRXB3AhuFugv61B-IiCZcAb_g7moUDKkymY8Y9V2ruI0Jvt3o3xfHE-xzrZtiSgFMAE; kuaishou.server.web_ph=9eab58a431c308cac5cfcc644147e03b0593',
'Host': 'www.kuaishou.com',
'Origin': 'https://www.kuaishou.com',
'Referer': 'https://www.kuaishou.com/profile/3xv78fxycm35nn4',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36',
}
# 請求引數 data
data = {
'operationName': "visionProfilePhotoList",
'query': "query visionProfilePhotoList($pcursor: String, $userId: String, $page: String, $webPageArea: String) {\n visionProfilePhotoList(pcursor: $pcursor, userId: $userId, page: $page, webPageArea: $webPageArea) {\n result\n llsid\n webPageArea\n feeds {\n type\n author {\n id\n name\n following\n headerUrl\n headerUrls {\n cdn\n url\n __typename\n }\n __typename\n }\n tags {\n type\n name\n __typename\n }\n photo {\n id\n duration\n caption\n likeCount\n realLikeCount\n coverUrl\n coverUrls {\n cdn\n url\n __typename\n }\n photoUrls {\n cdn\n url\n __typename\n }\n photoUrl\n liked\n timestamp\n expTag\n animatedCoverUrl\n stereoType\n videoRatio\n profileUserTopPhoto\n __typename\n }\n canAddComment\n currentPcursor\n llsid\n status\n __typename\n }\n hostName\n pcursor\n __typename\n }\n}\n",
'variables': {'userId': "3xv78fxycm35nn4", 'pcursor': "", 'page': "profile"}
}
data = json.dumps(data)
# <Response [200]>: 發送請求成功
response = requests.post(url=url, headers=headers, data=data)
json_data = response.json()
三. 決議資料 (獲取視頻地址 視頻標題)
feeds = json_data['data']['visionProfilePhotoList']['feeds']
for feed in feeds:
caption = feed['photo']['caption']
video_url = feed['photo']['photoUrl']
四. 發送請求 請求每個視頻地址 content: 圖片 視頻 音頻 二進制資料
video_data = requests.get(video_url).content
with open(f'video\\{caption}.mp4', mode='wb') as f:
f.write(video_data)
print(caption, '下載成功!!!')
五.總結
- 鏈接地址: https://www.kuaishou.com/graphql
- 請求方式: POST
- 請求頭(偽裝):
headers = {
'content-type': 'application/json',
'Cookie': '你自己的cookie',
'Host': 'www.kuaishou.com',
'Origin': 'https://www.kuaishou.com',
'Referer': 'https://www.kuaishou.com/profile/3xv78fxycm35nn4',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36' }
- 請求引數
data = {
'operationName': "visionProfilePhotoList",
'query': "query visionProfilePhotoList($pcursor: String, $userId: String, $page:
String, $webPageArea: String) {\n visionProfilePhotoList(pcursor: $pcursor, userId:
$userId, page: $page, webPageArea: $webPageArea) {\n result\n llsid\n webPageArea\n feeds {\n type\n author {\n id\n name\nfollowing\n headerUrl\n headerUrls {\n cdn\n url\n __typename\n }\n __typename\n }\n tags {\n type\n name\n __typename\n }\n photo {\n id\n duration\n caption\n likeCount\n realLikeCount\n coverUrl\n coverUrls {\n cdn\n url\n __typename\n }\n photoUrls {\n cdn\n url\n __typename\n }\n photoUrl\n liked\n timestamp\n expTag\n animatedCoverUrl\n stereoType\n videoRatio\n profileUserTopPhoto\n __typename\n }\n canAddComment\n currentPcursor\n llsid\n status\n __typename\n }\n hostName\n pcursor\n __typename\n }\n}\n",
'variables': {'userId': "3x9dquvtb9n9fps", 'pcursor': "", 'page': "profile"}
}
- 后續如果需要翻頁爬取,需要使用遞回實作


注:沒有基礎的同學可以暫時不用去實作,前面的內容能看懂就很不錯了哦
好了,我的這篇文章寫到這里就結束啦!
有更多建議或問題可以評論區或私信我哦!一起加油努力叭(? ?_?)?
喜歡就關注一下博主,或點贊收藏評論一下我的文章叭!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/396344.html
標籤:其他
