引言
由于部署瀏覽器爬蟲的機器瀏覽器版本不同,同時也不想因為部署了爬蟲導致影響系統瀏覽器數據,以及避免爬蟲過程中遇到的chrome與webdriver版本沖突。我決定將特定版本的chrome瀏覽器與webdriver下載到項目目錄內,同時chrome_driver在初始化時指定項目目錄內的chrome與webdriver。
下載指定版本的chrome與webdriver
選擇版本為:version: 123.0.6312.122 (r1262506)
機器為windows 64位系統,按照以下操作下載chrome與webdriver
chrome下載鏈接
chrome webdriver下載鏈接
chrome webdriver下載鏈接
下載完畢后將壓縮包解壓,將webdriver目錄下的chromedriver.exe文件放到chrome應用目錄內
移動過后可以時這樣放置,只要在項目的爬蟲通過相對路徑能訪問到即可
更新webdriver初始化代碼
# 指定chrome的位置
chrome_binary_path = r'./Chrome/chrome-win64/chrome.exe'
# 指定 Chrome 驅動的位置
chrome_driver_path = r'./Chrome/chromedriver.exe'def driver_init_new():############################ chrome指定版本與特定位置初始化 ############################# 創建 ChromeOptions 對象并設置 Chrome 應用程序位置chrome_options = webdriver.ChromeOptions()chrome_options.binary_location = chrome_binary_path# chrome_options.add_argument('--headless') # 無頭模式chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])# 禁止顯示瀏覽器窗口# chrome_options.add_argument('--window-position=-32000,-32000')# 創建 Service 對象chrome_service = webdriver.chrome.service.Service(chrome_driver_path)# 創建 Chrome 瀏覽器驅動對象,使用 options 和 service 參數browser = webdriver.Chrome(options=chrome_options, service=chrome_service)return browser
chrome_options.binary_location = chrome_binary_path
chrome_service = webdriver.chrome.service.Service(chrome_driver_path)
這兩行代碼指定特chrome的路徑與driver的位置,這樣就能直接使用我們剛剛配置的chrome瀏覽器用于爬蟲開發了。