欢迎来到赛兔子家园

Selenium规避网站监测

网站对Selenium采取监测

现在不少大网站有对selenium采取了监测机制。比如正常情况下我们用浏览器访问淘宝等网站的 window.navigator.webdriver的值为
undefined。而使用selenium访问则该值为true。

设置Chromedriver的启动参数即可解决问题。在启动Chromedriver之前,为Chrome开启实验性功能参数excludeSwitches它的值为['enable-automation']

完整代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'tian'
__data__ = '2021/2/24 17:06'

import time

from selenium import webdriver
from selenium.webdriver import ChromeOptions

# 创建option对象
option = ChromeOptions()
# 设置已开发者模式运行
option.add_experimental_option("excludeSwitches",["enable-automation"])

# 创建浏览器对象
driver = webdriver.Chrome(options=option)
driver.implicitly_wait(10)

try:
    driver.get("https://www.taobao.com/")
    time.sleep(2)

except Exception as error:
    print("报错信息:{0}".format(error))

finally:
    time.sleep(10)
    driver.quit()

 

posted on 2021-02-24 17:47  赛兔子  阅读(714)  评论(0编辑  收藏  举报

导航