Selenium自动化程序被检测为爬虫,怎么屏蔽和绕过

发布时间 2023-06-29 10:31:58作者: 随风_007

先打开浏览器,再链接操作

1、打开浏览器时添加以下参数:

--remote-debugging-port=9222 --user-data-dir="C:\\selenium\\ChromeProfile"

2、selenium中设置浏览器选项,通过上面设置的 9222端口连接浏览器:

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(options=chrome_options)

3、通过 subprocess 运行浏览器

当然,做自动化程序一般不会手工点击图标来打开浏览器,我们可以用命令行启动浏览器,然后再用 selenium 连接。

import subprocess
cmd = '"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" ' \\
'--remote-debugging-port=9222 ' \\
'--user-data-dir="C:\\selenium\\ChromeProfile"'

subprocess.run(cmd)