Python實現一個簡單的自動評論,自動點贊,自動關註腳本

来源:https://www.cnblogs.com/qshhl/archive/2022/05/29/16323888.html
-Advertisement-
Play Games

前言 今天的這個腳本,是一個別人發的外包,交互界面的代碼就不在這裡說了,但是可以分享下自動評論、自動點贊、自動關註、採集評論和視頻的數據是如何實現的 開發環境 python 3.8 運行代碼pycharm 2021.2 輔助敲代碼requests 第三方模塊 原理: 模擬客戶端,向伺服器發送請求 對 ...


前言

今天的這個腳本,是一個別人發的外包,交互界面的代碼就不在這裡說了,但是可以分享下自動評論、自動點贊、自動關註、採集評論和視頻的數據是如何實現的

開發環境

python 3.8 運行代碼
pycharm 2021.2 輔助敲代碼
requests 第三方模塊

原理:

模擬客戶端,向伺服器發送請求

對於本篇文章有疑問的同學可以加【資料白嫖、解答交流群:753182387】

代碼實現

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  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    __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. 獲取作品評論

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

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_userInfo(self, userId):
    """

    :param userId: 用戶ID
    :return: 用戶信息
    """
    json = {
        'operationName': "visionProfile",
        'query': "query visionProfile($userId: String) {\n  visionProfile(userId: $userId) {\n       hostName\n    userProfile {\n      ownerCount {\n        fan\n        photo\n        follow\n        photo_public\n        __typename\n      }\n      profile {\n        gender\n        user_name\n        user_id\n        headurl\n        user_text\n        user_profile_bg_url\n        __typename\n      }\n      isFollowing\n      __typename\n    }\n    __typename\n  }\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    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

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

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 第四章: 設計與聲明 ###18. 讓介面更容易被正確使用,不易被誤用 將你的class的public介面設計的符合class所扮演的角色,必要時不僅對傳參類型限制,還對傳參的值域進一步限制。 ###19. 設計class猶如設計type 內置類型如int、float等,本質也是一個class,用戶 ...
  • 前言 還記得你第一次遇到「線程安全」這個詞的時候嗎? 我第一次遇到線程安全這個詞是在學習多線程併發操作的時候,看到人家文章里出現這個詞,還有說各種線程安全的類,但是一開始並不理解線程安全是什麼意思,也沒去深究線程怎樣是安全的?怎樣是不安全的?只是腦子裡接收了這麼一個詞。 線程安全是多線程編程時的計算 ...
  • JVM 是Java的基石,Java從業者需要瞭解。JVM不是一個新的知識,網上文章很多,本篇的不同之處在於參考一手資料、內容經過反覆推敲。本文將會有篩選地研究JVM的精華部分,至少達到準系統架構師夠用的程度。本篇主要分享學習Java Class文件以及類載入器CLassLoader的知識。 ...
  • 資料庫表之間關係: 一對一 (可以看做一對多的特例) 一對多 多對多 下圖將涵蓋了所有關係。 根據restful介面風格,我們最終會落到一個實體上,示例按照b表。其他表同理。 GET https://ip:port/xx/xx/1/0/b 查詢的場景: 只需要b表的某些元素 需要b某些元素,及相關聯 ...
  • 前言 偶然一天把某項目文檔傳到手機上,用手機自帶的閱讀器方便隨時拿出來查閱。看著我那好久沒點開的閱讀器,再看著書架上擺著幾本不知道是多久之前導入的小說。 閉上眼,我仿佛看到那時候的自己。側躺著縮在被窩裡,亮度調到最低,看的津津有味。 睜開眼,一聲短嘆,心中五味雜陳,時間像箭一樣飛逝而去,過去靜止不動 ...
  • 狀態機簡介: 狀態機是有限狀態自動機的簡稱,是現實事物運行規則抽象而成的一個數學模型。【規則的抽象】 有限狀態機一般都有以下特點: (1)可以用狀態來描述事物,並且任一時刻,事物總是處於一種狀態; (2)事物擁有的狀態總數是有限的; (3)通過觸發事物的某些行為,可以導致事物從一種狀態過渡到另一種狀 ...
  • 我前面使用的jdbc和jdbc的工具類集成的但是它們在少部分代碼的情況下會會簡單,但是以後如果項目較大jdbc的固定代碼會很難維護,如果使用框架會簡單很多,也標志著java學習正式進入到框架階段 ...
  • Hi, 我是Mic。 今天分享一道一線互聯網公司必問的面試題。 ”JVM如何判斷一個對象可以被回收“ 關於這個問題,來看看普通人和高手的回答。 普通人: 嗯。。。。。。。。。。 高手: 好的,面試官。 在JVM裡面,要判斷一個對象是否可以被回收,最重要的是判斷這個對象是否還在被使用,只有沒被使用的對 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...