1.在服務器中安裝chrome
1 sudo apt-get install libxss1 libappindicator1 libindicator7 2 wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb 3 sudo dpkg -i google-chrome*.deb 4 sudo apt-get install -f
2.安裝scrapy
sudo apt-get install python3-scrapy
可能pip會熟悉一些,但是還是推薦使用這種方法。因為服務器可能並沒有內置pip3,而pip是給python2安裝的操作
3.一些非常麻煩的操作
-
關於chrome not reachable的問題
可能是在setting.py中關閉了cookie導致
-
安裝chromedriver
1 wget https://npm.taobao.org/mirrors/chromedriver/79.0.3945.36/chromedriver_linux64.zip 2 unzip chromedriver_linux64.zip
-
給予chromedriver軟連接
ln -s chromedriver的目錄 /usr/bin/chromedriver
這一步更多是將chromedriver與系統建立聯系,類似於windows的添加path(這一步我也不是很明白,如果有誰知道歡迎指正)
-
不可用root打開的解決方法
這里需要使用vim:# vim /usr/bin/google-chrome
並進行如下操作:
1 將 exec -a "$0" "$HERE/chrome" "$@" 改為 2 exec -a "$0" "$HERE/chrome" "$@" --no-sandbox $HOME
4.設置無界面化的chrome

1 def __init__(self, **kwargs): 2 super().__init__(**kwargs) 3 self.options = webdriver.ChromeOptions() 4 self.options.add_argument('--headless') 5 self.options.add_argument('--disable-gpu') 6 self.options.add_argument('--no-sandbox') 7 self.options.add_argument('blink-settings=imagesEnabled=false') 8 self.browser = webdriver.Chrome(chrome_options=self.options) 9 self.browser.set_page_load_timeout(30)
5.在后台執行的命令:
nohup python -u run.py > test.log 2>&1 & ps -A 可以查看進程
kill -9 [進程編號]終止后台
6.使用后台執行運行scrapy
首先要新建一個run.py在spider目錄里。
1 import os 2 3 if __name__ == '__main__': 4 os.system("scrapy crawl passage")
運行的時候用nohup執行run.py
nohup python -u run.py > test.log 2>&1 &