2.安裝selenium
pip install selenium
3.查看谷歌瀏覽器版本
打開(kāi)chrome瀏覽器,選擇設(shè)置->幫助->關(guān)于chrome,查看自己瀏覽器的版本
我的谷歌瀏覽器版本是 ‘版本 76.0.3809.100(正式版本) (32 位)’
4.下載驅(qū)動(dòng)程序chromedriver.exe
選擇自己瀏覽器對(duì)應(yīng)的版本下載,解壓后把文件‘chromedriver.exe’放到‘C:\Anaconda3\Scripts’
5.添加環(huán)境變量
修改:我的電腦->屬性->高級(jí)系統(tǒng)設(shè)置->環(huán)境變量->系統(tǒng)環(huán)境修改path:雙擊path->右邊按鈕添加文本,然后在最后面添加 ;‘C:\Anaconda3\Scripts‘
- 測(cè)試代碼
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
print(driver.title)
driver.quit()
只要輸出百度主頁(yè)標(biāo)題即可
7.無(wú)頭瀏覽器使用
在做爬蟲(chóng)時(shí),通常是不需要打開(kāi)瀏覽器的,只需要使用瀏覽器的內(nèi)核,因此可以使用Chrome的無(wú)頭模式,下面是demo
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("http://www.baidu.com")
print(driver.title)
driver.close()