在瀏覽器進行爬取網頁,模擬登陸的時候可能需selenium模擬瀏覽器的行為
在此我是用的是谷歌瀏覽器,python的編程環境
第一步
需要在python環境中安裝selenium
pip install selenium
第二步
下載谷歌瀏覽器相應的軟件,軟件鏈接
下載相應版本的軟件然后放到谷歌瀏覽器下的目錄
C:\Program Files (x86)\Google\Chrome\Application
第三步
在其它教程里,需要添加第二步的軟件到環境變量中,但是在測試中發現不好用,在此介紹另一種方法
在python編輯器中輸入以下代碼進行測試
from selenium import webdriver
chrome_driver = 'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe'
browser = webdriver.Chrome(executable_path=chrome_driver)
browser.get('https://www.baidu.com/')
需要注意的是,只要指定第二步下載的軟件的路徑正確,如上例,
chrome_driver = 'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe'
就可以正常運行