前言
嗨喽~大家好呀,这里是魔王呐 ❤ ~!
刷到的视频怕它下架?我们来采集保存一下它
知识点:
-
动态数据抓包
-
requests发送请求
-
json数据解析
开发环境:
-
python 3.8 运行代码
-
pycharm 2022.3 辅助敲代码
-
requests pip install requests
代码展示
需要 源码 . 素材 . 点击领取即可
导入模块
import re
import requests # 发送请求
伪装
headers = {
'Cookie': 'kpf=PC_WEB; clientid=3; did=web_4bf90ac4df8aaef2d99527f1da1063fd; userId=3075864129; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqAB9tSqSgbeN3Qhc4H_V6Awn1-yfEnaceVEJQUhtxgCBBHHQl_DyWKPYDO3XIdjFQTHwSSFOPZL6FPTk7LSW1dfp7qvBSeP4ANjPT1evIoq9ODcPJyb6YgEIdsV-Dzm_Sq73yQg6jYhvuRNJGwAM_GfAfUKWiG4RDdGDpHd1uCAJd0qM2xZwqzr11JlIz3yRFeNFNfO2zINkJAktY8lUyQwuRoS9XMBYg26NCtIxdOwhbHEY-u6IiBNGgmRMQXPmCodWM1RM7oFpjHSgD61betJSR00w0VYtSgFMAE; kuaishou.server.web_ph=69e54c87baac46f523262858017c7988cf22; kpn=KUAISHOU_VISION',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
}
请求链接
url = 'https://www.****.com/graphql'
pcursor = ""
while True:
# 请求体
json = {
'operationName': "visionProfilePhotoList",
'query': "fragment photoContent on PhotoEntity {\n id\n duration\n caption\n originCaption\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 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",
'variables': {'userId': "3xjfq9eesf4kxwg", 'pcursor': pcursor, 'page': "profile"}
}
- 发送请求
response = requests.post(url=url, headers=headers, json=json)
- 获取数据
json_data = response.json()
- 解析数据
键: 值 通过:前面的内容 取:后面的内容
feeds = json_data['data']['visionProfilePhotoList']['feeds']
pcursor = json_data['data']['visionProfilePhotoList']['pcursor']
for feed in feeds:
photoUrl = feed['photo']['photoUrl']
caption = feed['photo']['caption']
caption = re.sub('[\\\/:*?"<>|]', '', caption)
print(caption, photoUrl)
- 保存数据
video_data = requests.get(photoUrl).content
with open(rf"C:\Users\Administrator\Desktop\video\{caption}.mp4", mode='wb') as f:
f.write(video_data)
if pcursor == "no_more":
break
效果展示
尾语 💝
要成功,先发疯,下定决心往前冲!
学习是需要长期坚持的,一步一个脚印地走向未来!
未来的你一定会感谢今天学习的你。
—— 心灵鸡汤
本文章到这里就结束啦~感兴趣的小伙伴可以复制代码去试试哦 😝