Python教程

用Python批量爬取快手视频,实现自动关注/点赞/评论

本文主要是介绍用Python批量爬取快手视频,实现自动关注/点赞/评论,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

今天来点特别的~

不仅把好看的视频全部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    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",
        "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    result\n    commentId\n    content\n    timestamp\n    status\n    __typename\n  }\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    result\n    __typename\n  }\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       followStatus\n    hostName\n    error_msg\n    __typename\n  }\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批量爬取快手视频,实现自动关注/点赞/评论的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!