在無界面模式下發現請求地址全部報404,在有界面的模式下就是正常運行的。在網上查了半天,發現這種情況,由於爬取的網站進行了selenium反爬蟲導致的。
1.嘗試使用開啟開發者模式
opt = webdriver.ChromeOptions()
# 把chrome設置成無界面模式,不論windows還是linux都可以,自動適配對應參數
opt.set_headless()
# 創建chrome無界面對象
opt.add_argument("--start-maximized") # 界面設置最大化
# opt.add_argument('no-sandbox')
opt.add_argument('--headless')
opt.add_argument('--disable-gpu')
opt.add_experimental_option('excludeSwitches', ['enable-automation'])#開啟開發者模式
driver = webdriver.Chrome(options=opt)
運行之后還是會報404,這種方法放棄
2.嘗試使用firefix瀏覽器
opt=webdriver.FirefoxOptions()
opt.set_headless()
opt.add_argument("--start-maximized")
opt.add_argument('--headless')
opt.add_argument('--disable-gpu')
driver = webdriver.Firefox(options=opt)
成功解決問題,在無界面模式下可以爬取,
https://github.com/mozilla/geckodriver/releases/ 該鏈接可以下載新的火狐瀏覽器驅動
