如何隐藏Selenium特征

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType

# 亿牛云爬虫加强版代理IP 地址、端口号、用户名和密码
proxy_address = 'www.16yun.cn'
proxy_port = '3100'
proxy_username = '16YUN'
proxy_password = '16IP'

# 设置Chrome选项,包括隐藏Selenium特征、设置代理IP和排除或关闭一些Selenium相关开关
options = webdriver.ChromeOptions()
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument('--disable-extensions')
options.add_argument('--disable-gpu')
options.add_argument('--disable-infobars')
options.add_argument('--disable-notifications')
options.add_argument('--disable-popup-blocking')
options.add_argument('--disable-web-security')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--no-sandbox')
options.add_argument('--start-maximized')
options.add_argument('--user-data-dir=/dev/null')
options.add_argument('--proxy-server={}'.format(proxy_address + ':' + proxy_port))
options.add_argument('--proxy-auth={}:{}'.format(proxy_username, proxy_password))
options.add_experimental_option('excludeSwitches', ['enable-automation', 'useAutomationExtension'])

# 初始化Chrome浏览器,并使用上述选项
driver = webdriver.Chrome(options=options)

# 隐藏navigator.webdriver标志,将其值修改为false或undefined
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
    'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
})

# 设置user-agent,改变user-agent的值
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
driver.execute_cdp_cmd("Network.setUserAgentOverride", {"userAgent": user_agent})

# 访问大众点评中商品的评论页面
url = 'https://www.dianping.com/shop/1234567/review_all'
driver.get(url)

 

posted @ 2023-07-01 10:14  随风_007  阅读(132)  评论(0)    收藏  举报