selenium被检测

1.使用火狐浏览器(解决一部分)

from selenium.webdriver import Firefox
from selenium import webdriver

#禁止一些东西加载提升速度可能也能解决一些爬去过程中的bug和反爬吧!
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("permissions.default.image",2)
firefox_profile.set_preference("thatoneguydotnet.QuickJava.curVersion", "2.0.6.1")
firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Images", 2)
firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.AnimatedImage", 2)  
firefox_profile.set_preference("browser.cache.disk.enable", False)
firefox_profile.set_preference("browser.cache.memory.enable", False)
firefox_profile.set_preference("browser.cache.offline.enable", False)
firefox_profile.set_preference("network.http.use-cache", False)
driver = Firefox()
driver.get('http://www.gxgkzy.cn/Login.aspx',firefox_profile=firefox_profile)

2.使用undetected_chromedriver库(建议,解决一大部分检测)

建议指定一下浏览器驱动不然这玩意老报错

安装 :pip install git+https://github.com/ultrafunkamsterdam/undetected-chromedriver.git

import undetected_chromedriver as uc

driver = uc.Chrome(use_subprocess=True,driver_executable_path='chromedriver.exe')
#driver = uc.Chrome(use_subprocess=True)
driver.maximize_window()
driver.get('http://www.gxgkzy.cn/Login.aspx')
sleep(10)
driver.close()

建议使用下面的方式运行,我不懂为什么,用就完了,或者可以翻源码!

import undetected_chromedriver as uc

if __name__ == '__main__':
    driver = uc.Chrome(driver_executable_path='chromedriver.exe')
    #driver = uc.Chrome()
    driver.maximize_window()
    driver.get('http://www.gxgkzy.cn/Login.aspx')
    sleep(10)
    driver.close()

3.方法二在很多情况下问题很多!

from selenium.webdriver.chrome.options import Options
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = Options()
#options.add_argument('--headless')
prefs={"profile.managed_default_content_settings.images":2,'permissions.default.stylesheet': 2}
options.add_experimental_option("prefs", prefs)  # 禁止加载图片和css
options.set_capability('acceptInsecureCerts',True)#跳过安全证书认证
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument("--disable-blink-features=AutomationControlled")
driver=Chrome(service=Service(ChromeDriverManager().install()),
                    options=options)
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
                            Object.defineProperty(navigator, 'webself.driver', {
                              get: () => undefined
                            })
                          """
    })
    driver.implicitly_wait(10)
driver.get('https://www.youzy.cn/tzy/search/colleges/collegeList')

你可能感兴趣的:(selenium,firefox,测试工具)