lsp:用Python把精彩的视频批量保存本地,顺便给博主自动来个关注、点赞、评论三连

x33g5p2x  于2022-05-27 转载在 Python  
字(5.2k)|赞(0)|评价(0)|浏览(661)

序言

是我太久没发了吗?昨天没人看,那么今天来点特别的~

不仅把好看的视频全部pa下来,咱们还要实现自动评论、点赞、关注三连~

宝,你也可以顺手给我个三连吗?给你个摸摸大~

抓包分析流程

我写成了文档,都在这个PDF里面了,但是好像不能上传,所以点一下大家自行下载吧!
点我获取,提取密码 qwer

开始代码

获取视频的代码

  1. import requests # 发送请求 第三方模块(第三方应用 pip)
  2. import re
  3. # 伪装
  4. # 1. 选中要替换的代码
  5. # 2. ctrl + R
  6. # 3. 第一个框(.*?): (.*)
  7. # 4. 在第二个框里面输入 '$1': '$2',
  8. # 5. 点击全部替换(* 点亮 * 号)
  9. headers = {
  10. 'content-type': 'application/json',
  11. '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',
  12. 'Host': 'www.***.com',
  13. 'Origin': 'https://www.***.com',
  14. # 防盗链
  15. 'Referer': 'https://www.kuaishou.com/profile/3xhv7zhkfr3rqag',
  16. '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',
  17. }
  18. url = 'https://www.***.com/graphql'
  19. def get_page(pcursor):
  20. # 指定要谁的视频
  21. data = {
  22. "operationName": "visionProfilePhotoList",
  23. "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",
  24. "variables": {"userId": "3x2vsxyxbbwcjta", "pcursor": pcursor, "page": "profile"}
  25. }
  26. # 1. 发送请求 get post
  27. response = requests.post(url=url, headers=headers, json=data)
  28. # <Response [200]>: 请求成功
  29. # 2. 获取数据 .json() 返回字典类型数据
  30. # .text: 拿到的就是 文本内容 python数据类型 字符串 > 字典类型 > 键值对(拼音)方式取值
  31. json_data = response.json()
  32. # 3. 解析数据
  33. # 新华字典 = {'A': '啊', 'B': '不', 'C': '从'}
  34. # 新华字典['B'] python数据容器 存储数据
  35. # 正则
  36. feeds = json_data['data']['visionProfilePhotoList']['feeds']
  37. pcursor = json_data['data']['visionProfilePhotoList']['pcursor']
  38. for feed in feeds:
  39. photoUrl = feed['photo']['photoUrl']
  40. caption = feed['photo']['caption']
  41. # 正则替换
  42. # 第一个参数里面是需要替换的一些字符
  43. # 第二个参数 是把这些字符替换为 空
  44. # 第三个参数 是需要替换的变量
  45. # \\ : \
  46. # \/ : /
  47. caption = re.sub('[\\\/:*?"<>|\n]', '', caption)
  48. print(caption, photoUrl)
  49. # 4. 保存数据 如果你们拿到的链接 就是 视频 或者 音频 或者 图片
  50. # .content: 获取视频(音频 / 图片) 二进制数据
  51. video_data = requests.get(photoUrl).content
  52. # 视频名称
  53. # wb 以二进制覆盖写入
  54. with open(f'video/{caption}.mp4', mode='wb') as f:
  55. f.write(video_data)
  56. # 递归: 2.出口
  57. if pcursor == "no_more":
  58. # 退出?
  59. return
  60. # 递归: 1.自己调用自己
  61. get_page(pcursor)
  62. get_page("")

自动评论

  1. def post_comment(self, content, photoAuthorId, photoId):
  2. """
  3. :param content: 评论内容
  4. :param photoAuthorId: 该作品的作者id
  5. :param photoId: 作品id
  6. :return: 有没有成功
  7. """
  8. json = {
  9. 'operationName': "visionAddComment",
  10. '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",
  11. 'variables': {
  12. 'content': content,
  13. 'expTag': "1_a/2005158523885162817_xpcwebsearchxxnull0",
  14. 'photoAuthorId': photoAuthorId,
  15. 'photoId': photoId
  16. }
  17. }
  18. response = requests.post(url=self.url, json=json, headers=self.headers)
  19. json_data = response.json()
  20. print(json_data)
  21. return json_data

自动点赞

  1. def is_like(self, photoId, photoAuthorId):
  2. """
  3. :param photoId: 作品id
  4. :param photoAuthorId: 该作品的作者id
  5. :return: 有没有成功
  6. """
  7. json = {
  8. 'operationName': "visionVideoLike",
  9. '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}",
  10. 'variables': {
  11. 'cancel': 0,
  12. 'expTag': "1_a/2005158523885162817_xpcwebsearchxxnull0",
  13. 'photoAuthorId': photoAuthorId,
  14. 'photoId': photoId
  15. }
  16. }
  17. response = requests.post(url=self.url, json=json, headers=self.headers)
  18. json_data = response.json()
  19. print(json_data)
  20. return json_data

自动关注

  1. def is_follow(self, touid):
  2. """
  3. :param touid: 用户id
  4. :return:
  5. """
  6. json = {
  7. 'operationName': "visionFollow",
  8. '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",
  9. 'variables': {
  10. 'expTag': "1_a/2005158523885162817_xpcwebsearchxxnull0",
  11. 'followSource': 3,
  12. 'ftype': 1,
  13. 'touid': touid
  14. }
  15. }
  16. response = requests.post(url=self.url, json=json, headers=self.headers)
  17. json_data = response.json()
  18. print(json_data)
  19. return json_data

代码不会操作,也有详细的视频讲解,可以左侧扫码获取~

溜了溜了,今天就到这里,兄弟们快去试试吧!

相关文章