通过Python在网站自动发送评论

通过selenium来实现,如果没有安装Python和Selenium,需要先安装,以下是我的环境测试通过的脚本。

环境信息

python版本。

1
2
python --version
Python 3.10.8

selenium 版本。

1
2
3
4
5
6
7
8
9
10
11
pip show selenium
Name: selenium
Version: 4.14.0
Summary:
Home-page: https://www.selenium.dev
Author:
Author-email:
License: Apache 2.0
Location: /usr/local/lib/python3.10/site-packages
Requires: certifi, trio, trio-websocket, urllib3
Required-by:

自动发送评论程序

以在抖音直播间发送评论为例,程序分为两个部分首先登录后获取用户cookies ,再次通过获取的cookies来自动发送评论。

set_cookies.py代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from selenium import webdriver
import time
import pickle

from selenium.webdriver import ChromeOptions
from selenium.webdriver.chrome.service import Service
# -----------
browser = webdriver.Chrome(service=Service(r'/Users/wangdongsheng/PycharmProjects/studyselenium/chromedriver-mac-x64/chromedriver'))
option = ChromeOptions()
option.add_argument(
'user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.4844.74 Safari/537.36"'
)
option.add_experimental_option('excludeSwitches', ['enable-automation']) # 防止系统检测到自动化工具
option.add_experimental_option('useAutomationExtension', False)
browser = webdriver.Chrome(service=Service(r'/Users/wangdongsheng/PycharmProjects/studyselenium/chromedriver-mac-x64/chromedriver'),options=option)
browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
})
browser.maximize_window() # 页面最大化
# --------------

# 设置最大等待时长为 10秒
browser.implicitly_wait(20)
browser.get('https://www.douyin.com/')
print(browser.get_cookies())
time.sleep(1)
print(browser.get_cookies())
print("字典长度:\n",len(browser.get_cookies()),browser.get_cookies())
input("登入抖音账号后,请输入任意键继续...")


time.sleep(5)
with open("djangowang.cookies",'wb') as file:
pickle.dump(browser.get_cookies(),file)
print("字典长度:\n",len(browser.get_cookies()),browser.get_cookies())


input("请输入任意键继续...")
#browser.delete_all_cookies()
#time.sleep(1)
#print("字典长度:\n",len(browser.get_cookies()),browser.get_cookies())

use_cookies.py,自动发送评论代码。因为我用的是Chrome需要提前下载好与浏览器对应的驱动( https://googlechromelabs.github.io/chrome-for-testing/ ) ,驱动放到指定的位置后,见以下代码中(./chromedriver-mac-x64/chromedriver)这一行为驱动路径。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from selenium import webdriver
import time
from selenium.webdriver import ChromeOptions
from selenium.webdriver.chrome.service import Service
import pickle
import random
import sys


with open("djangowang.cookies",'rb') as file:
cookiesList = pickle.load(file)

# 发送请求时设置一些头文件,主要用于伪装
browser = webdriver.Chrome(service=Service(r'./chromedriver-mac-x64/chromedriver'))
option = ChromeOptions()
option.add_argument(
'user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.4844.74 Safari/537.36"'
)
option.add_experimental_option('excludeSwitches', ['enable-automation']) # 防止系统检测到自动化工具
option.add_experimental_option('useAutomationExtension', False)
browser = webdriver.Chrome(service=Service(r'./chromedriver-mac-x64/chromedriver'),options=option)
browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
})
browser.maximize_window() # 页面最大化
# --------------

browser.get('https://www.douyin.com/')
# input("请输入任意键继续...")

# 加载cookies
for cookie in cookiesList:
browser.add_cookie(cookie)

# 主播房间号
browser.get('https://live.douyin.com/476806339740')
time.sleep(5)

# 随机发送评论的内容
remarks = [
'楼主正能量!!!',
'楼主正能量,楼主正能量,楼主正能量,楼主正能量!!!',
'主播正能量!!!',
'主播帅气!!!',
'主播牛!!!',
'最棒的主播,带给我们欢乐的声音,让我们的心情更舒畅!!!!',
'主播最棒,一声温暖的问候,让我们温柔的心开始跳动!!!!',
'落落晨星,斜雨竹林,皆不如姑娘眉眼动人',
'您好!!!',
'花一些时间,总会看清一些事。用一些事情,总会看清一些人!!!',
'求抱抱',
'求握手!!!',
'求相拥!!!',
'点点关注谢谢!!!',
]



while True:
textElement = browser.find_element('xpath','//textarea[@class="webcast-chatroom___textarea"]')
textElement.clear()
textElement.send_keys(random.choice(remarks)) # 输入新字符串
sendElement = browser.find_element('xpath','//button[@class="webcast-chatroom___send-btn"][@type="button"]')
time.sleep(2.5)
sendElement.click()


# input("请输入任意键继续...")
browser.quit()

效果图