8步,用python实现进行自动化评论、点赞、关注脚本

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

嗨害大家好鸭 我是小熊猫~

分享这个没啥就是好玩

这里写目录标题

在这里插入图片描述


开发环境

  • python 3.8 运行代码
  • pycharm 2021.2
  • requests 第三方模块

代码实现

点击此处跳转文末名片获取

在这里插入图片描述

1.请求伪装

def __init__(self):
    self.headers = {
        'content-type': 'application/json',
        'Cookie':
        '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  id\n  duration\n  caption\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  animatedCoverUrl\n  distance\n  videoRatio\n  liked\n  stereoType\n  profileUserTopPhoto\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     photo {\n    ...photoContent\n    __typename\n  }\n  canAddComment\n  llsid\n  status\n  currentPcursor\n  __typename\n}\n\nquery visionSearchPhoto($keyword: String, $pcursor: String, $searchSessionId: String, $page: String, $webPageArea: String) {\n  visionSearchPhoto(keyword: $keyword, pcursor: $pcursor, searchSessionId: $searchSessionId, page: $page, webPageArea: $webPageArea) {\n    result\n    llsid\n    webPageArea\n    feeds {\n      ...feedContent\n      __typename\n    }\n    searchSessionId\n    pcursor\n    aladdinBanner {\n      imgUrl\n      link\n      __typename\n    }\n    __typename\n  }\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. 获取作品评论ID采集

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    commentCount\n        rootComments {\n      commentId\n      authorId\n      authorName\n      content\n      headurl\n      timestamp\n      likedCount\n      realLikedCount\n      liked\n      status\n        subCommentsPcursor\n      subComments {\n        commentId\n        authorId\n        authorName\n        content\n        headurl\n        timestamp\n        likedCount\n        realLikedCount\n        liked\n        status\n        replyToUserName\n        replyTo\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\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    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

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,  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

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       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

7. 获取创作者视频

def get_video(self, userId, pcursor):
    """
    :param userId: 用户id
    :param pcursor: 页码
    :return: 作品
    """
    json = {
        'operationName': "visionProfilePhotoList",
        'query': "fragment photoContent on PhotoEntity {\n    duration\n  caption\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  __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  __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': 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

8. 调用函数

if __name__ == '__main__':
    kuaishou = KuaiShou()
    kuaishou.get_comments('3xzry7secwhunai', '')
    kuaishou.post_comment('0', '3xgz9zaku7hig96', '3xydesqbvtrvcuq')
    kuaishou.is_like('3xydesqbvtrvcuq', '3xgz9zaku7hig96')
    kuaishou.is_follow('3xxhfqquuachnje')
    kuaishou.get_userInfo('3xxhfqquuachnje')
    kuaishou.get_video('3xxhfqquuachnje', '')

今天的文章就到这里啦~

我是小熊猫咱下篇文章再见啦(✿◡‿◡)

在这里插入图片描述

👇问题解答 · 源码获取 · 技术交流 · 抱团学习请联系👇
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: python