背景:在爬取一个页面时,开始用到selenium+chromedriver时,页面打开空白,但是chrome浏览器打开页面正常,调查了一下是因为chromedriver和chrome浏览器访问网站时指纹不一致导致的这个问题。所以就用到了undetected_chromedriver
安装undetected_chromedriver
pip install undetected_chromedriver
使用undetected_chromedriver
from undetected_chromedriver import Chrome, ChromeOptionsoptions = ChromeOptions()# 设置浏览器为无头浏览
options.add_argument("--headless")# 创建 Chrome 对象
with Chrome(options=options) as driver:driver.get("http://xxx.com")page_source = driver.page_source