大家在用Python做爬蟲的時(shí)候,會(huì)經(jīng)常用到phantomjs。
但是我們一般都是用chrome/firefox查看效果,再轉(zhuǎn)成phantomjs。
一方面這樣比較費(fèi)時(shí)間,另一方面phantomjs不太穩(wěn)定,而且官方已經(jīng)停止維護(hù)了。
所以趕緊投身大Google的懷抱吧!
如果用 yum install google-chrome -y 不能裝上的話就嘗試下載安裝
下載chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
可能會(huì)遇到網(wǎng)路問題,你可以先下載到本地再上傳
安裝chrome
yum install ./google-chrome-stable_current_x86_64.rpm
配置chromedriver
注意chromedriver的版本,要與你安裝的chrome版本對(duì)應(yīng)上,這里的版本已經(jīng)不是最新的。
版本列表:http://chromedriver.chromium.org/downloads
下載chromedriver_linux64.zip
wget https://chromedriver.storage.googleapis.com/2.38/chromedriver_linux64.zip
解壓chromedriver_linux64.zip
unzip chromedriver_linux64.zip
為chromedriver授權(quán)
chmod 755 chromedriver
Python代碼測(cè)試
from selenium import webdriver
def spider(url='http://bing.com'):
option = webdriver.ChromeOptions()
option.add_argument('--no-sandbox')
option.add_argument('--headless')
# 注意path
driver = webdriver.Chrome(executable_path='../chromedriver', chrome_options=option)
driver.get(url)
print(driver.page_source)
spider()
這個(gè)命令禁止沙箱模式,否則肯能會(huì)報(bào)錯(cuò)遇到chrome異常。
option.add_argument('--no-sandbox')