""" 反反爬 -- driver.get(网页) -- 人工登录后关闭程序 -- driver.get(网页) """ # 1. 普通浏览器:不能记录登录信息,容易触发反爬机制 # from selenium.webdriver import Chrome # # with Chrome() as driver: # driver.get("https://www.douyin.com/") import os import time from selenium.webdriver import Chrome, ChromeOptions from selenium.webdriver.common.by import By options = ChromeOptions() # 根据系统选择Windows或Linux设置 dir = r"C:\Users\%s\AppData\Local\Google\Chrome\User Data" % os.getlogin() # Windows # dir = "/home/%s/.config/google-chrome" % os.getlogin() # Linux options.add_argument("user-data-dir=" + dir) # 设置用户数据目录 options.add_argument("--disable-blink-features=AutomationControlled") # 禁用启用Blink运行时的功能 options.add_experimental_option("excludeSwitches", ["enable-automation"]) # 去除浏览器检测框 with Chrome(options=options) as driver: driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """}) # 再次覆盖window.navigator.webdriver的值 # driver.get("https://www.douyin.com/") # 直接打开抖音直播间 driver.get( "https://live.douyin.com/248465901420?enter_from_merge=web_search&enter_method=web_card&function_type=&live_type=game&previous_page=&request_id=20221129154150010202086073440248DE&search_params=%7B%22search_type%22%3A%22live%22%2C%22impr_id%22%3A%2220221129154150010202086073440248DE%22%2C%22search_id%22%3A%2220221129154150010202086073440248DE%22%2C%22search_keyword%22%3A%22%E7%88%AA%E5%93%87%E8%80%81%E8%8B%8D%22%2C%22search_result_id%22%3A%227171311045196123422%22%7D&seat_fit_status=off") # 根据弹幕输入框的class "webcast-chatroom___textarea"获取输入框 input_message = driver.find_element(By.XPATH, '//textarea[@class="webcast-chatroom___textarea"]') # 根据弹幕发送按钮的class "webcast-chatroom___send-btn"获取发送按钮 button_send = driver.find_element(By.XPATH, '//button[@class="webcast-chatroom___send-btn"]') while True: # 输入框输入弹幕 input_message.send_keys("苍老师666") # 点击发送按钮发送弹幕 button_send.click() # 如果设置两秒间隔,一段时间会触发抖音反爬机制输入验证码 time.sleep(6)
标签:get,反反,22%,driver,send,python,抖音,import,options From: https://www.cnblogs.com/xmgcool/p/16936962.html