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