現在很多網站添加了檢測selenium的工具,但selenium也有反檢測的類,就是selenium中的ChromeOptions類
代碼如下:
from selenium import webdriver
import time
# 實現無可視化界面
from selenium.webdriver.chrome.options import Options
# 實現規避檢測
from selenium.webdriver import ChromeOptions
# 實現無可視化界面
chrome_options=Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# 實現規避檢測
option=ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
# 無頭瀏覽器 + 反檢測
wd=webdriver.Chrome(executable_path='./chromedriver.exe',chrome_options=chrome_options,option=option)
wd.get('https://www.baidu.com')
print(wd.page_source)
time.sleep(5)
wd.quit()