基于Python實(shí)現(xiàn)批量保存視頻到本地
序言
是我太久沒發(fā)了嗎?昨天沒人看,那么今天來點(diǎn)特別的~
不僅把好看的視頻全部pa下來,咱們還要實(shí)現(xiàn)自動評論、點(diǎn)贊、關(guān)注三連~
寶,你也可以順手給我個(gè)三連嗎?給你個(gè)摸摸大~

開始代碼
獲取視頻的代碼
import requests # 發(fā)送請求 第三方模塊(第三方應(yīng)用 pip)
import re
# 偽裝
# 1. 選中要替換的代碼
# 2. ctrl + R
# 3. 第一個(gè)框(.*?): (.*)
# 4. 在第二個(gè)框里面輸入 '$1': '$2',
# 5. 點(diǎn)擊全部替換(* 點(diǎn)亮 * 號)
headers = {
'content-type': 'application/json',
'Cookie': 'kpf=PC_WEB; kpn=KUAISHOU_VISION; clientid=3; did=web_ea128125517a46bd491ae9ccb255e242; client_key=65890b29; didv=1646739254078; userId=270932146; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABctRgGaXi5efEBpnbdtJMp3nnnXqENRWBoFQABtOr1ZFUNAjEo5NTZ4F0leSypsSFE4_-FGTnBqKEYh8Wcrszm3FGF03559Z9bFQCX_8ew_kLKPWVB9ZRlWQgISoG4-XZXIOqiBgkQKsPbpYKiA3X4_0rMDbo9-c0nWXeOoThekj8W3u7_yfI4fUY3h5WgTEDYT0yrXkZmhWlFV_jpVxDrBoSzFZBnBL4suA5hQVn0dPKLsMxIiCo1i0bY9V6-OVEk7yMnH86RNliTZACHvLPjL9FTHHQOigFMAE; kuaishou.server.web_ph=09735672944cbf9e53431bf3e0514a0d058b',
'Host': 'www.***.com',
'Origin': 'https://www.***.com',
# 防盜鏈
'Referer': 'https://www.kuaishou.com/profile/3xhv7zhkfr3rqag',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36',
}
url = 'https://www.***.com/graphql'
def get_page(pcursor):
# 指定要誰的視頻
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": "3x2vsxyxbbwcjta", "pcursor": pcursor, "page": "profile"}
}
# 1. 發(fā)送請求 get post
response = requests.post(url=url, headers=headers, json=data)
# <Response [200]>: 請求成功
# 2. 獲取數(shù)據(jù) .json() 返回字典類型數(shù)據(jù)
# .text: 拿到的就是 文本內(nèi)容 python數(shù)據(jù)類型 字符串 > 字典類型 > 鍵值對(拼音)方式取值
json_data = response.json()
# 3. 解析數(shù)據(jù)
# 新華字典 = {'A': '啊', 'B': '不', 'C': '從'}
# 新華字典['B'] python數(shù)據(jù)容器 存儲數(shù)據(jù)
# 正則
feeds = json_data['data']['visionProfilePhotoList']['feeds']
pcursor = json_data['data']['visionProfilePhotoList']['pcursor']
for feed in feeds:
photoUrl = feed['photo']['photoUrl']
caption = feed['photo']['caption']
# 正則替換
# 第一個(gè)參數(shù)里面是需要替換的一些字符
# 第二個(gè)參數(shù) 是把這些字符替換為 空
# 第三個(gè)參數(shù) 是需要替換的變量
# \\ : \
# \/ : /
caption = re.sub('[\\\/:*?"<>|\n]', '', caption)
print(caption, photoUrl)
# 4. 保存數(shù)據(jù) 如果你們拿到的鏈接 就是 視頻 或者 音頻 或者 圖片
# .content: 獲取視頻(音頻 / 圖片) 二進(jìn)制數(shù)據(jù)
video_data = requests.get(photoUrl).content
# 視頻名稱
# wb 以二進(jìn)制覆蓋寫入
with open(f'video/{caption}.mp4', mode='wb') as f:
f.write(video_data)
# 遞歸: 2.出口
if pcursor == "no_more":
# 退出?
return
# 遞歸: 1.自己調(diào)用自己
get_page(pcursor)
get_page("")
自動評論
def post_comment(self, content, photoAuthorId, photoId):
"""
:param content: 評論內(nèi)容
:param photoAuthorId: 該作品的作者id
:param photoId: 作品id
:return: 有沒有成功
"""
json = {
'operationName': "visionAddComment",
'query': "mutation visionAddComment($photoId: String, $photoAuthorId: String, $content: String, $replyToCommentId: ID, $replyTo: ID, $expTag: String) { (photoId: $photoId, photoAuthorId: $photoAuthorId, content: $content, replyToCommentId: $replyToCommentId, replyTo: $replyTo, expTag: $expTag) {\n result\n commentId\n content\n timestamp\n status\n __typename\n }\n}\n",
'variables': {
'content': content,
'expTag': "1_a/2005158523885162817_xpcwebsearchxxnull0",
'photoAuthorId': photoAuthorId,
'photoId': photoId
}
}
response = requests.post(url=self.url, json=json, headers=self.headers)
json_data = response.json()
print(json_data)
return json_data
自動點(diǎn)贊
def is_like(self, photoId, photoAuthorId):
"""
:param photoId: 作品id
:param photoAuthorId: 該作品的作者id
:return: 有沒有成功
"""
json = {
'operationName': "visionVideoLike",
'query': "mutation visionVideoLike($photoId: String, $photoAuthorId: String, $cancel: Int, $expTag: String) {\n visionVideoLike(photoId: $photoId, photoAuthorId: $photoAuthorId, cancel: $cancel, expTag: $expTag) {\n result\n __typename\n }\n}",
'variables': {
'cancel': 0,
'expTag': "1_a/2005158523885162817_xpcwebsearchxxnull0",
'photoAuthorId': photoAuthorId,
'photoId': photoId
}
}
response = requests.post(url=self.url, json=json, headers=self.headers)
json_data = response.json()
print(json_data)
return json_data
自動關(guān)注
def is_follow(self, touid):
"""
:param touid: 用戶id
:return:
"""
json = {
'operationName': "visionFollow",
'query': "mutation visionFollow($touid: String, $ftype: Int, $followSource: Int, $expTag: String) {\n visionFollow(touid: $touid, ftype: $ftype, followSource: $followSource, expTag: $expTag) {\n followStatus\n hostName\n error_msg\n __typename\n }\n}\n",
'variables': {
'expTag': "1_a/2005158523885162817_xpcwebsearchxxnull0",
'followSource': 3,
'ftype': 1,
'touid': touid
}
}
response = requests.post(url=self.url, json=json, headers=self.headers)
json_data = response.json()
print(json_data)
return json_data
以上就是基于Python實(shí)現(xiàn)批量保存視頻到本地的詳細(xì)內(nèi)容,更多關(guān)于Python保存視頻的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python數(shù)據(jù)容器dict(字典)的實(shí)現(xiàn)
本文主要介紹了Python數(shù)據(jù)容器dict(字典)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
python從list列表中選出一個(gè)數(shù)和其對應(yīng)的坐標(biāo)方法
今天小編就為大家分享一篇python從list列表中選出一個(gè)數(shù)和其對應(yīng)的坐標(biāo)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Django中l(wèi)ogin_required裝飾器的深入介紹
這篇文章主要給大家介紹了關(guān)于Django中l(wèi)ogin_required裝飾器的使用方法,并給大家進(jìn)行了實(shí)例借鑒,利用@login_required實(shí)現(xiàn)Django用戶登陸訪問限制,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11
如何解決requests,已經(jīng)安裝卻無法import問題
這篇文章主要介紹了如何解決requests,已經(jīng)安裝卻無法import問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06

