今天来点特别的~
不仅把好看的视频全部pa下来,咱们还要实现自动评论、点赞、关注三连~
宝,你也可以顺手给我个三连吗?给你个摸摸大~
我写成了文档,都在这个PDF里面了,但是好像不能上传,所以点一下大家自行下载吧!
点我获取,提取密码 qwer
获取视频的代码
import requests # 发送请求 第三方模块(第三方应用 pip) import re # 伪装 # 1. 选中要替换的代码 # 2. ctrl + R # 3. 第一个框(.*?): (.*) # 4. 在第二个框里面输入 '$1': '$2', # 5. 点击全部替换(* 点亮 * 号) # Python学习交流群 279199867 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 resultn llsidn webPageArean feeds {n typen author {n idn namen followingn headerUrln headerUrls {n cdnn urln __typenamen }n __typenamen }n tags {n typen namen __typenamen }n photo {n idn durationn captionn likeCountn realLikeCountn coverUrln coverUrls {n cdnn urln __typenamen }n photoUrls {n cdnn urln __typenamen }n photoUrln likedn timestampn expTagn animatedCoverUrln stereoTypen videoRation profileUserTopPhoton __typenamen }n canAddCommentn currentPcursorn llsidn statusn __typenamen }n hostNamen pcursorn __typenamen }n}n", "variables": {"userId": "3x2vsxyxbbwcjta", "pcursor": pcursor, "page": "profile"} } # 1. 发送请求 get post response = requests.post(url=url, headers=headers, json=data) # <Response [200]>: 请求成功 # 2. 获取数据 .json() 返回字典类型数据 # .text: 拿到的就是 文本内容 python数据类型 字符串 > 字典类型 > 键值对(拼音)方式取值 json_data = response.json() # 3. 解析数据 # 新华字典 = {'A': '啊', 'B': '不', 'C': '从'} # 新华字典['B'] python数据容器 存储数据 # 正则 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('[\/:*?"<>|n]', '', caption) print(caption, photoUrl) # 4. 保存数据 如果你们拿到的链接 就是 视频 或者 音频 或者 图片 # .content: 获取视频(音频 / 图片) 二进制数据 video_data = requests.get(photoUrl).content # 视频名称 # wb 以二进制覆盖写入 with open(f'video/{caption}.mp4', mode='wb') as f: f.write(video_data) # 递归: 2.出口 if pcursor == "no_more": # 退出? return # 递归: 1.自己调用自己 get_page(pcursor) get_page("")
自动评论
def post_comment(self, content, photoAuthorId, photoId): """ :param content: 评论内容 :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 resultn commentIdn contentn timestampn statusn __typenamen }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
自动点赞
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 resultn __typenamen }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
自动关注
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 followStatusn hostNamen error_msgn __typenamen }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批量爬取快手视频,实现自动关注/点赞/评论 - Python技术站