最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

selenium+chromedriver在服務(wù)器運(yùn)行的詳細(xì)教程

 更新時間:2020年03月31日 15:05:39   作者:woodcol  
這篇文章主要介紹了selenium+chromedriver在服務(wù)器運(yùn)行的詳細(xì)方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1.前言

想使用selenium從網(wǎng)站上抓數(shù)據(jù),但有時候使用phantomjs會出錯。chrome現(xiàn)在也有無界面運(yùn)行模式了,以后就可以不用phantomjs了。

但在服務(wù)器安裝chrome時出現(xiàn)了一些錯誤,這里總結(jié)一下整個安裝過程

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

這時應(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以免報錯。
之后可以再打開一個ssh連接到服務(wù)器,使用命令行訪問服務(wù)器的本地的9222端口:

curl http://localhost:9222

如果安裝好了,會看到調(diào)試信息。但我這里會報一個錯誤,下邊是錯誤的解決辦法。

1)可能的錯誤解決方法

運(yùn)行完上邊的命令可能會報一個不能在root下運(yùn)行chrome的錯誤。這個時候使用下邊方設(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ū)動chromedriver

下載chromedriver

chromedriver提供了操作chrome的api,是selenium控制chrome的橋梁。

chromedriver最好安裝最新版的,記的我一開始安裝的不是最新版的,會報一個錯。用最新版的chromedriver就沒有問題,最新版的可以在下邊地址找到
https://sites.google.com/a/chromium.org/chromedriver/downloads

我寫這個文章時最新版是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中的第三項設(shè)置參數(shù),使用這個設(shè)置可以避免網(wǎng)站檢測到你使用的是無界模式進(jìn)行反抓取。

下邊另外的兩項設(shè)置,不進(jìn)行設(shè)置時在桌面版linux系統(tǒng),或者mac系統(tǒng)上會打開有界面的chrome.調(diào)試時可以注釋掉下邊兩行使用有界面版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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

安国市| 东港市| 集安市| 桂东县| 秦皇岛市| 仁怀市| 华容县| 贡嘎县| 南溪县| 隆回县| 讷河市| 湄潭县| 安徽省| 忻州市| 高要市| 黎城县| 台山市| 涿州市| 工布江达县| 青浦区| 开封县| 江源县| 桃源县| 阿坝| 独山县| 汝阳县| 牟定县| 顺昌县| 兴义市| 张家口市| 上蔡县| 湛江市| 华池县| 香河县| 平度市| 长寿区| 静宁县| 藁城市| 鄂托克旗| 丰县| 当涂县|