1. 程式人生 > 其它 >用Python實現在網站上自動評論!鍵盤俠都噴不贏你!

用Python實現在網站上自動評論!鍵盤俠都噴不贏你!

自從上次在B站看到一個噴子,一個人噴一堆人,當時我就看不過去了,直接用Python寫了個自動評論軟體,他說一句我能說十句,當場教育噴子~

於是乎,順便整理一下,做了一手教程,分享給大家,當然不是教大家去做噴子,只是學學這麼個技術~


不知道你們用的什麼環境,我一般都是用的Python3.6環境和pycharm直譯器,沒有軟體,或者沒有資料,沒人解答問題,都可以加這個群點我免費領取資料 包括今天的程式碼,過幾天我還會做個視訊教程出來,有需要也可以領取~
給大家準備的學習資料包括但不限於:
Python 環境、pycharm編輯器/永久啟用/翻譯外掛
python 零基礎視訊教程
Python 介面開發實戰教程
Python 爬蟲實戰教程
Python 資料分析實戰教程
python 遊戲開發實戰教程
Python 電子書100本
Python 學習路線規劃


模組:

requests >>> pip install requests
    re
    time (時間模組 會給一個延時)
    random (隨機) >>> 隨機評論內容
    內建模組  你只需要安裝好python環境 自帶的

實現案例思路:

一. 資料來源分析(爬蟲)

先手動去評論一下 (網站傳送請求的情況是什麼的)
找請求url地址 評論傳送請求地址
通過開發者工具進行抓包分析
post請求: 需要提交一個表單

[評論多個視訊,比如這個up主所有視訊,都進行評論]

1. 通過開發者工具進行抓包分析: 可以找到評論請求地址 [oid引數變化: 每一個視訊都對應一個oid]
2. 分析 oid 引數的來源 >>> 在視訊詳情頁的網頁原始碼裡面就有 oid 引數
3. 每個視訊詳情頁的url 都是有一個bv號 BV1764y1e7eu
4. 找到所有視訊的 bv 號 [找視訊的ID] 在視訊列表找到 視訊的BV號

二. 程式碼實現

1. 傳送請求 對於視訊列表頁傳送請求
2. 獲取資料 獲取json字典資料
3. 解析資料 提取 視訊Bv號
4. 傳送請求 對於視訊詳情頁url地址傳送請求
5. 獲取資料 網頁原始碼 視訊裡面 oid 引數
6. 實現評論
7. 進行多個/全部 視訊評論


來 直接上程式碼

詳細解釋我都打在註釋了,今天容我懶一波~

import requests  # 資料請求模組  pip install requests
import pprint  # 格式化輸出的模組 內建模組
import re  # 正則表示式 內建模組
import random  #
隨機模組 內建模組 import time # 時間模組 內建模組 Python學習群872937351 headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36' } def get_response(html_url): """傳送請求""" # headers請求頭 作用? 把python程式碼偽裝成瀏覽器 # cookie: 主要是用檢測是否有登入賬號 使用者資訊 # user-agent: 瀏覽器基本資訊 response = requests.get(url=html_url, headers=headers) return response def get_video_bv(html_url): """獲取視訊BV號""" response = get_response(html_url) # 獲取json字典資料 json_data = response.json() # 解析資料 json直接解析提取 根據冒號左邊的內容 提取冒號右邊的內容 v_list = json_data['data']['list']['vlist'] # 列表推導式 v_list = [i['bvid'] for i in v_list] # lis = [] # v_list 是一個列表 列表裡面每一個元素都是一個字典 # 想要獲取列表中的每個元素 通過遍歷 for迴圈 i就是字典 # for i in v_list: # lis.append(i['bvid']) # print(v_list) # pprint.pprint(response.json()) return v_list def get_video_oid(video_bv_id): """獲取視訊的oid引數""" # f'{}' 字串的格式化方法 '{page}'.format(page) # bv 號傳入url地址當中 video_url = f'https://www.bilibili.com/video/{video_bv_id}' response = get_response(video_url) # 函式返回值 \d+ 匹配\d 匹配一個數字 \d+ 是匹配多個數字 # 正則表示式 oid # <script>window.__INITIAL_STATE__={"aid":(762391044), # () 精確匹配 表示我要的內容就是括號裡面的內容 每一個視訊的oid都不一樣 # \d 表示的匹配一個數字 \d+ 表示匹配多個數字 .*? 表示匹配任意字元 # .*? oid = re.findall('<script>window\.__INITIAL_STATE__={"aid":(.*?)', response.text)[0] return oid def comment(oid): """評論""" comment_list = ['6666', 'up主牛皮', 'python牛皮', '牛皮'] content = random.choice(comment_list) comment_url = 'https://api.bilibili.com/x/v2/reply/add' data = { 'oid': oid, 'type': '1', 'message': content, 'plat': 1, 'ordering': 'heat', 'jsonp': 'jsonp', 'csrf': '0f085ffe952fc8658bfae7a34de1b1d6' } response = requests.post(url=comment_url, data=data, headers=headers) status_code = response.status_code # 獲取狀態碼 return status_code def main(html_url): """主函式""" v_list = get_video_bv(html_url=html_url) for index in v_list: time.sleep(2) oid = get_video_oid(index) status_code = comment(oid) if status_code == 200: print(f'{index}評論成功') else: print(f'{index}評論失敗') if __name__ == '__main__': for page in range(1, 15): print('稍等五秒鐘') time.sleep(5) url = f'https://api.bilibili.com/x/space/arc/search?mid=16682415&ps=30&tid=0&pn={page}&keyword=&order=pubdate&jsonp=jsonp' main(url)