import requests
import re
from tqdm import tqdm
import os
headers = {
'Cookie': 'kpf=PC_WEB; clientid=3; did=web_61e5b5e7adca8eea2626a61be73b50a1; userId=4100742766; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABAGaLFJvMMgVaccJJzqAkgViIRwwp14c1VZSDc5PFElWac4U7mG6bWWitA9tfme4LhNzdQphlgK7TxQekHs7VBYuIlFrh6I5A2r3pEIOqc-1U_CdBmjXEdOXkCq7UNGeGuFlwsWrvt1Lbl2yheDDZwuNwZ6EZiaYgBFWRadU6Xx2f3BsxjfO8FnezbX3dOFJXq1NQzMC5FoPw1G9D_hu-IBoSsguEA2pmac6i3oLJsA9rNwKEIiB_u1Yx_eCKKpXrczSoYdUJ_jdvW1rCcAQRlllOTTUnGigFMAE; kuaishou.server.web_ph=fea1c65a7702456d9e4a426c5d775014590a; kpn=KUAISHOU_VISION',
'Host': 'www.kuaishou.com',
'Origin': 'https://www.kuaishou.com',
'Referer': 'https://www.kuaishou.com/profile/3x2zjsnqdxsu5ak',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
'content-type': 'application/json'
}
number = 1
userId = '3xu7xq5uemgewkw'
def get_page(pcursor):
global number
payload = {
"operationName": "visionProfilePhotoList",
"variables": {"userId": userId, "pcursor": pcursor, "page": "profile"},
"query": "fragment photoContent on PhotoEntity {\n __typename\n id\n duration\n caption\n originCaption\n likeCount\n viewCount\n commentCount\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 riskTagContent\n riskTagUrl\n}\n\nfragment recoPhotoFragment on recoPhotoEntity {\n __typename\n id\n duration\n caption\n originCaption\n likeCount\n viewCount\n commentCount\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 riskTagContent\n riskTagUrl\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 ...recoPhotoFragment\n __typename\n }\n canAddComment\n llsid\n status\n currentPcursor\n tags {\n type\n name\n __typename\n }\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"
}
if pcursor is None:
return 0
url = "https://www.kuaishou.com/graphql"
res = requests.post(url, headers=headers, json=payload)
feeds = res.json()['data']['visionProfilePhotoList']['feeds']
pcursor = res.json()['data']['visionProfilePhotoList']['pcursor']
for feed in feeds:
name = feed['author']['name']
mp4_url = feed['photo']['videoResource']['h264']['adaptationSet'][0]['representation'][0]['url']
music_path = f'D:/短视频/快手/{name}'
os.makedirs(music_path, exist_ok=True)
if 'm3u8' in mp4_url:
m3u8_list = requests.get(mp4_url).text
ts_files = re.sub('#.*', '', m3u8_list).split()
url = mp4_url.split('/')[:-1]
url = '/'.join(url) + '/'
for ts in ts_files:
ts_url = url + ts
response_size = int(requests.head(ts_url).headers.get('content-length', 0))
with tqdm(total=response_size, desc=f"正在下载---> ts {name}-{number}", unit='B', unit_scale=True,
unit_divisor=1024) as pbar:
with requests.get(ts_url, stream=True) as r:
with open(f'{music_path}/{number}.mp4', 'ab') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
pbar.update(len(chunk))
number += 1
pbar.close()
else:
response_size = int(requests.head(mp4_url).headers.get('content-length', 0))
with tqdm(total=response_size, desc=f"正在下载---> {name}-{number}", unit='B', unit_scale=True,
unit_divisor=1024) as pbar:
with requests.get(mp4_url, stream=True) as r:
with open(f'{music_path}/{number}.mp4', 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
pbar.update(len(chunk))
number += 1
pbar.close()
get_page(pcursor)
def main():
get_page('')
if __name__ == '__main__':
main()
python-快手-博主作品存储本地
最新推荐文章于 2024-09-07 20:08:36 发布