栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python实现一个简单的自动评论,自动点赞,自动关注脚本

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python实现一个简单的自动评论,自动点赞,自动关注脚本

前言

今天的这个脚本,是一个别人发的外包,交互界面的代码就不在这里说了,但是可以分享下自动评论、自动点赞、自动关注、采集评论和视频的数据是如何实现的

开发环境

python 3.8 运行代码
pycharm 2021.2 辅助敲代码
requests 第三方模块

原理:

模拟客户端,向服务器发送请求

代码实现 1. 请求伪装
def __init__(self):
    self.headers = {
        'content-type': 'application/json',
        'Cookie': 'kpf=PC_WEB; kpn=KUAISHOU_VISION; clientid=3; did=web_ea128125517a46bd491ae9ccb255e242; client_key=65890b29; didv=1646739254078; _bl_uid=pCldq3L00L61qCzj6fytnk2wmhz5; userId=270932146; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABH2BHihXp4liEYWMBFv9aguyfs8BsbINQIWqgoDw0SimMkpXwM7PKpKdJcZbU12QOyeKFaG4unV5EUkkEswL0HnA8_A9z2ujLlKN__gRsxU2B5kIYgirTDPiVJ3uPN1sU9mqvog3auoNJxDdbKjVeFNK1wQ5HTM_yUvYvmWOx9iC8IKcvnmo9YnG_J9ske-t-wiCWMgSCA25HN6MRqCMxuhoSnIqSq99L0mk4jolsseGdcwiNIiC8rjheuewIA1Bk3LwkNIYikU2zobcuvgAiBbMnBuDixygFMAE; kuaishou.server.web_ph=55c7e6b2033ea94a3447ea98082642cd6f1a',
        'Host': 'www.ks.com',
        'Origin': 'https://www.ks.com',
        'Referer': 'https://www.ks.com/search/video?searchKey=%E9%BB%91%E4%B8%9D',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36',
    }
    self.url = 'https://www.ks.com/graphql'
2. 获取搜索内容的方法
def get_search(self, keyword, pcursor):
    """
    :param keyword: 关键词
    :param pcursor: 页码
    :return: 搜索作品
    """
    json = {
        'operationName': "visionSearchPhoto",
        'query': "fragment photoContent on PhotoEntity {n  idn  durationn  captionn  likeCountn  viewCountn  realLikeCountn  coverUrln  photoUrln  photoH265Urln  manifestn  manifestH265n  videoResourcen  coverUrls {n    urln    __typenamen  }n  timestamp  n  animatedCoverUrln  distancen  videoRation  likedn  stereoTypen  profileUserTopPhoton  __typenamen}nnfragment feedContent on Feed {n  typen  author {n    idn    namen    headerUrln    followingn    headerUrls {n      urln      __typenamen    }n    __typenamen  }n  photo {n    ...photoContentn    __typenamen  }n  canAddCommentn  llsidn  statusn  currentPcursorn  __typenamen}nnquery visionSearchPhoto($keyword: String, $pcursor: String, $searchSessionId: String, $page: String, $webPageArea: String) {n  visionSearchPhoto(keyword: $keyword, pcursor: $pcursor, searchSessionId: $searchSessionId, page: $page, webPageArea: $webPageArea) {n    resultn    llsidn    webPageArean    feeds {n      ...feedContentn      __typenamen    }n    searchSessionIdn    pcursorn    aladdinBanner {n      imgUrln      linkn      __typenamen    }n    __typenamen  }n}n",
        'variables': {'keyword': keyword, 'pcursor': pcursor, 'page': "search"}
    }
    response = requests.post(url=self.url, json=json, headers=self.headers)
    json_data = response.json()
    print(json_data)
    return json_data
3. 获取作品评论
def get_comments(self, photoId, pcursor):
    """
    :param photoId: 作品id
    :param pcursor: 页码
    :return: 评论内容
    """
    json = {
        'operationName': "commentListQuery",
        'query': "query commentListQuery($photoId: String, $pcursor: String) {  visionCommentList(photoId: $photoId, pcursor: $pcursor) {n    commentCountn        rootComments {n      commentIdn      authorIdn      authorNamen      contentn      headurln      timestampn      likedCountn      realLikedCountn      likedn      statusn      subCommentCountn      subCommentsPcursorn      subComments {n        commentIdn        authorIdn        authorNamen        contentn        headurln        timestampn        likedCountn        realLikedCountn        likedn        statusn        replyToUserNamen        replyTon        __typenamen      }n      __typenamen    }n    __typenamen  }n}n",
        'variables': {'photoId': photoId, 'pcursor': pcursor}
    }
    response = requests.post(url=self.url, json=json, headers=self.headers)
    json_data = response.json()
    print(json_data)
    return json_data
4. 自动评论
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
5. 点赞操作
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
6. 关注操作
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
7. 获取创作者信息
def get_userInfo(self, userId):
    """

    :param userId: 用户ID
    :return: 用户信息
    """
    json = {
        'operationName': "visionProfile",
        'query': "query visionProfile($userId: String) {n  visionProfile(userId: $userId) {n       hostNamen    userProfile {n      ownerCount {n        fann        photon        follown        photo_publicn        __typenamen      }n      profile {n        gendern        user_namen        user_idn        headurln        user_textn        user_profile_bg_urln        __typenamen      }n      isFollowingn      __typenamen    }n    __typenamen  }n}n",
        'variables': {'userId': userId}
    }
    response = requests.post(url=self.url, json=json, headers=self.headers)
    json_data = response.json()
    print(json_data)
    return json_data
8. 获取创作者视频
def get_video(self, userId, pcursor):
    """
    :param userId: 用户id
    :param pcursor: 页码
    :return: 作品
    """
    json = {
        'operationName': "visionProfilePhotoList",
        'query': "fragment photoContent on PhotoEntity {n    durationn  captionn  likeCountn  viewCountn  realLikeCountn  coverUrln  photoUrln  photoH265Urln  manifestn  manifestH265n  videoResourcen  coverUrls {n    urln    __typenamen  }n  timestampn  expTagn  animatedCoverUrln  distancen  videoRation  likedn  stereoTypen  profileUserTopPhoton  __typenamen}nnfragment feedContent on Feed {n  typen  author {n    idn    namen    headerUrln    followingn    headerUrls {n      urln      __typenamen    }n    __typenamen  }n  photo {n    ...photoContentn    __typenamen  }n  canAddCommentn  llsidn  statusn  currentPcursorn  __typenamen}nnquery 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      ...feedContentn      __typenamen    }n    hostNamen    pcursorn    __typenamen  }n}n",
        'variables': {'userId': userId, 'pcursor': pcursor, 'page': "profile"}
    }
    response = requests.post(url=self.url, json=json, headers=self.headers)
    json_data = response.json()
    print(json_data)
    return json_data
9. 调用函数
if __name__ == '__main__':
    kuaishou = KuaiShou()
    # 获取评论
    kuaishou.get_comments('3xzry7secwhunai', '')
    # 发布评论
    kuaishou.post_comment('爱你', '3xgz9zaku7hig96', '3xydesqbvtrvcuq')
    # 点赞
    kuaishou.is_like('3xydesqbvtrvcuq', '3xgz9zaku7hig96')
    # 关注
    kuaishou.is_follow('3xxhfqquuachnje')
    # 创作者信息
    kuaishou.get_userInfo('3xxhfqquuachnje')
    # 获取创作者作品
    kuaishou.get_video('3xxhfqquuachnje', '')

Python玩弄快手视频:批量爬取、自动点赞、自动评论、自动关注

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934833.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号