selenium+chromedriver在服務(wù)器運(yùn)行的詳細(xì)教程
1.
想使用selenium從網(wǎng)站上抓數(shù)據(jù),但有時(shí)候使用phantomjs會出錯(cuò)。chrome現(xiàn)在也有無界面運(yùn)行模式了,以后就可以不用phantomjs了。
但在服務(wù)器安裝chrome時(shí)出現(xiàn)了一些錯(cuò)誤,這里總結(jié)一下整個(gè)安裝過程
2.ubuntu上安裝chrome
# Install Google Chrome # https://askubuntu.com/questions/79280/how-to-install-chrome-browser-properly-via-command-line sudo apt-get install libxss1 libappindicator1 libindicator7 wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb sudo dpkg -i google-chrome*.deb # Might show "errors", fixed by next line sudo apt-get install -f
這時(shí)應(yīng)該已經(jīng)安裝好了,用下邊的命行運(yùn)行測試一下:
google-chrome --headless --remote-debugging-port=9222 https://chromium.org --disable-gpu
這里是使用headless模式進(jìn)行遠(yuǎn)程調(diào)試,ubuntu上大多沒有g(shù)pu,所以–disable-gpu以免報(bào)錯(cuò)。
之后可以再打開一個(gè)ssh連接到服務(wù)器,使用命令行訪問服務(wù)器的本地的9222端口:
curl http://localhost:9222
如果安裝好了,會看到調(diào)試信息。但我這里會報(bào)一個(gè)錯(cuò)誤,下邊是錯(cuò)誤的解決辦法。
1)可能的錯(cuò)誤解決方法
運(yùn)行完上邊的命令可能會報(bào)一個(gè)不能在root下運(yùn)行chrome的錯(cuò)誤。這個(gè)時(shí)候使用下邊方設(shè)置一下chrome
1.找到google-chrome文件
我的位置位于/opt/google/chrome/
2.用vi打開google-chrome文件
vi /opt/google/chrome/google-chrome
在文件中找到
exec -a "$0" "$HERE/chrome" "$@"
3.在后面添加 –user-data-dir –no-sandbox即可,整條shell命令就是
exec -a "$0" "$HERE/chrome" "$@" --user-data-dir --no-sandbox
4.再重新打開google-chrome即可正常訪問!
3.安裝chrome驅(qū)動(dòng)chromedriver
下載chromedriver
chromedriver提供了操作chrome的api,是selenium控制chrome的橋梁。
chromedriver最好安裝最新版的,記的我一開始安裝的不是最新版的,會報(bào)一個(gè)錯(cuò)。用最新版的chromedriver就沒有問題,最新版的可以在下邊地址找到
https://sites.google.com/a/chromium.org/chromedriver/downloads
我寫這個(gè)文章時(shí)最新版是2.37
wget https://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
到這里服務(wù)器端的無界面版chrome就安裝好了。
4.無界面版chrome使用方法
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument("user-agent='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'")
wd = webdriver.Chrome(chrome_options=chrome_options,executable_path='/home/chrome/chromedriver')
wd.get("https://www.163.com")
content = wd.page_source.encode('utf-8')
print content
wd.quit()
這里chrome_options中的第三項(xiàng)設(shè)置參數(shù),使用這個(gè)設(shè)置可以避免網(wǎng)站檢測到你使用的是無界模式進(jìn)行反抓取。
下邊另外的兩項(xiàng)設(shè)置,不進(jìn)行設(shè)置時(shí)在桌面版linux系統(tǒng),或者mac系統(tǒng)上會打開有界面的chrome.調(diào)試時(shí)可以注釋掉下邊兩行使用有界面版chrome來調(diào)試程序。
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
5.參考
https://jiayi.space/post/zai-ubuntufu-wu-qi-shang-shi-yong-chrome-headless
https://blog.csdn.net/u013703963/article/details/71083802
總結(jié)
到此這篇關(guān)于selenium+chromedriver在服務(wù)器運(yùn)行的文章就介紹到這了,更多相關(guān)selenium+chromedriver在服務(wù)器運(yùn)行內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信