Python Selenium 抖音直播平台实现自动发送评论
首先运行 test_01_save_cookies.py 手动登入个人的抖音账号获得浏览器的 cookies, 并以pickle文件的格式保存到当前目录下.然后打开文件 test_02_use_cookies.py将第15行代码中的URL链接改成所需要的抖音主播链接,修改完成后,运行即可实现为自己喜爱的主播打call.test_01_save_cookies.pyfrom selenium imp
·
首先运行 test_01_save_cookies.py 手动登入个人的抖音账号
获得浏览器的 cookies, 并以pickle文件的格式保存到当前目录下.
然后打开文件 test_02_use_cookies.py
将第15行代码中的URL链接改成所需要的抖音主播链接,
修改完成后,运行即可实现为自己喜爱的主播打call.
test_01_save_cookies.py
from selenium import webdriver
import time
import pickle
browser = webdriver.Chrome()
# 设置最大等待时长为 10秒
browser.implicitly_wait(10)
browser.get('https://www.douyin.com/')
time.sleep(1)
print("字典长度:\n",len(browser.get_cookies()),browser.get_cookies())
input("登入抖音账号后,请输入任意键继续...")
time.sleep(1)
with open("抖音cookies文件.pickle",'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())
test_02_use_cookies.py
from selenium import webdriver
import time
import pickle
import random
# https://www.douyin.com/user/MS4wLjABAAAAbon_VQY_4dB7dzuBf8Roykgr9umNW5vXhj5hEBDFzbE?enter_method=video_title&author_id=100407188539&group_id=6977211498972056835&log_pb=%7B%22impr_id%22%3A%22021628698872157fdbddc0100fff0030a1030e700000091b342ad%22%7D&enter_from=video_detail
with open("抖音cookies文件.pickle",'rb') as file:
cookiesList = pickle.load(file)
browser = webdriver.Chrome()
browser.get('https://www.douyin.com/')
# input("请输入任意键继续...")
for cookie in cookiesList:
browser.add_cookie(cookie)
browser.get('https://live.douyin.com/2732898268')
# https://live.douyin.com/2732898268
# https://live.douyin.com/512037658897
time.sleep(1)
muteSwitches = browser.find_elements_by_xpath('//xg-icon[@class="xgplayer-volume"][@data-state="mute"]')
for muteSwitche in muteSwitches:
print("___---"*10)
muteSwitche.click()
remarks = [
'太强了!!!',
'太精彩了!!!',
'主播帅气!!!',
'主播技术强!!!',
'太好看了!!!',
'主播牛!!!',
'666技术太强了!!!',
]
while True:
textElement = browser.find_element_by_xpath('//textarea[@class="webcast-chatroom___textarea"]')
textElement.clear()
textElement.send_keys(random.choice(remarks)) # 输入新字符串
sendElement = browser.find_element_by_xpath('//button[@class="webcast-chatroom___send-btn"][@type="button"]')
time.sleep(0.5)
sendElement.click()
# input("请输入任意键继续...")
browser.quit()
运行效果:
更多推荐
已为社区贡献3条内容
所有评论(0)