import requests
import json
import os
import re
from concurrent.futures import ThreadPoolExecutor
# 需要下载用户的id
# 例如点进去某个用户后,浏览器地址https://www.kuaishou.com/profile/3x3zb2597vbu2fc最后面的就是这个用户的id
userId="3xn3jcnkwipvfkq"
# 登录完成网页快手后去f12找找cookie
cookie=""
# 创建保存视频的文件夹
e = "快手视频"
if not os.path.exists(e):
os.mkdir(e)
url_ = "https://www.kuaishou.com/graphql"
#头部信息最好自己找一下,粘贴过来
headers1 = {
"accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Length": "1176",
"content-type": "application/json",
# Cookie要登录下载自己的
"Cookie":cookie,
"Host": "www.kuaishou.com",
"Origin": "https://www.kuaishou.com",
"Referer": "https://www.kuaishou.com/profile/{}".format(userId),
"sec-ch-ua": '"Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": 'Windows',
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844."
"51 Sa"
"fari/537.36"
}
headers2 = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
}
urls = [] #视频链接地址
names = [] #视频名字
# for循环获取五页的视频url和标题
# 并将他们存进urls列表和names列表中
pcursor = ''
while 1:
data={"operationName":"visionProfilePhotoList","variables":{"userId":userId,"pcursor":pcursor,"page":"profile"},"query":"fragment photoContent on PhotoEntity {\n id\n duration\n caption\n likeCount\n viewCount\n realLikeCount\n coverUrl\n photoUrl\n photoH265Url\n manifest\n manifestH265\n videoResource\n coverUrls {\n url\n __typename\n }\n timestamp\n expTag\n animatedCoverUrl\n distance\n videoRatio\n liked\n stereoType\n profileUserTopPhoto\n musicBlocked\n __typename\n}\n\nfragment feedContent on Feed {\n type\n author {\n id\n name\n headerUrl\n following\n headerUrls {\n url\n __typename\n }\n __typename\n }\n photo {\n ...photoContent\n __typename\n }\n canAddComment\n llsid\n status\n currentPcursor\n __typename\n}\n\nquery 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 ...feedContent\n __typename\n }\n hostName\n pcursor\n __typename\n }\n}\n"}
# print(datas)
data = json.dumps(data)
res = requests.post(url=url_, headers=headers1, data=data)
json_data = res.json()
# print(json_data)
feed_list = json_data["data"]["visionProfilePhotoList"]['feeds']
# print(feed_list)
print("成功")
for feed in feed_list:
url = feed['photo']['photoUrl']
# print(url)
name = feed['photo']['caption']
id=feed['photo']['id']
new_name = re.sub(r'[\\/:*?"<>|\n]', '_', name)
urls.append(url)
names.append(new_name+"____"+id)
pcursor = json_data["data"]["visionProfilePhotoList"]['pcursor']
print(pcursor)
if "no_more"==pcursor:
break
# 定义:视频url,和视频标题为形参的函数,以元组形式传参数
def download(zipp):
(href, title) = zipp
response = requests.get(url=href, headers=headers2).content
with open(e + "/" + title + ".mp4", mode="wb") as f:
f.write(response)
print('保存完成:', title)
zips = zip(urls, names)
pool = ThreadPoolExecutor(8)
for zip_ in zips:
pool.submit(download, zip_)
pool.shutdown(wait=True)
网友评论