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

開始代碼
獲取視頻的代碼
import requests # 發(fā)送請(qǐng)求 第三方模塊(第三方應(yīng)用 pip)
import re
# 偽裝
# 1. 選中要替換的代碼
# 2. ctrl + R
# 3. 第一個(gè)框(.*?): (.*)
# 4. 在第二個(gè)框里面輸入 '$1': '$2',
# 5. 點(diǎn)擊全部替換(* 點(diǎn)亮 * 號(hào))
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ā)送請(qǐng)求 get post
response = requests.post(url=url, headers=headers, json=data)
# <Response [200]>: 請(qǐng)求成功
# 2. 獲取數(shù)據(jù) .json() 返回字典類型數(shù)據(jù)
# .text: 拿到的就是 文本內(nèi)容 python數(shù)據(jù)類型 字符串 > 字典類型 > 鍵值對(duì)(拼音)方式取值
json_data = response.json()
# 3. 解析數(shù)據(jù)
# 新華字典 = {'A': '啊', 'B': '不', 'C': '從'}
# 新華字典['B'] python數(shù)據(jù)容器 存儲(chǔ)數(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("")
自動(dòng)評(píng)論
def post_comment(self, content, photoAuthorId, photoId):
"""
:param content: 評(píng)論內(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
自動(dòng)點(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
自動(dòng)關(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保存視頻的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用django-guardian實(shí)現(xiàn)django-admin的行級(jí)權(quán)限控制的方法
這篇文章主要介紹了使用django-guardian實(shí)現(xiàn)django-admin的行級(jí)權(quán)限控制的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
導(dǎo)入tensorflow時(shí)報(bào)錯(cuò):cannot import name ''abs''的解決
這篇文章主要介紹了導(dǎo)入tensorflow時(shí)報(bào)錯(cuò):cannot import name 'abs'的解決,文中介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
tensorboard 可以顯示graph,卻不能顯示scalar的解決方式
今天小編就為大家分享一篇tensorboard 可以顯示graph,卻不能顯示scalar的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
python?隨時(shí)間序列變動(dòng)畫圖的方法
這篇文章主要介紹了python?基礎(chǔ)繪圖之關(guān)于隨時(shí)間序列變動(dòng)的圖的畫法,首先大家要明白畫圖需要考慮的問題,如何在圖中適當(dāng)?shù)娘@示軸標(biāo)簽的樣式和數(shù)量,詳細(xì)代碼跟隨小編一起看看吧2022-01-01
python3+opencv3識(shí)別圖片中的物體并截取的方法
今天小編就為大家分享一篇python3+opencv3識(shí)別圖片中的物體并截取的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
淺談Python3.10 和 Python3.9 之間的差異
多年來,Python 進(jìn)行了大量升級(jí),并且在新版本中添加了許多功能。本文就詳細(xì)的介紹 一下Python3.10 和 Python3.9差異,感興趣的朋友可以了解一下2021-09-09
用python實(shí)現(xiàn)日志文件,并且按時(shí)間命名文件名方式
這篇文章主要介紹了用python實(shí)現(xiàn)日志文件,并且按時(shí)間命名文件名方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08

