1. 程式人生 > >4.簡單爬蟲——selenium的環境安裝 Ubuntu環境

4.簡單爬蟲——selenium的環境安裝 Ubuntu環境

該內容僅供學習,如有錯誤,歡迎指出

selenium [1] 是一個用於Web應用程式測試的工具。Selenium測試直接執行在瀏覽器中,就像真正的使用者在操作一樣。支援的瀏覽器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。這個工具的主要功能包括:測試與瀏覽器的相容性——測試你的應用程式看是否能夠很好得工作在不同瀏覽器和作業系統之上。測試系統功能——建立迴歸測試檢驗軟體功能和使用者需求。支援自動錄製動作和自動生成 .Net、Java、Perl等不同語言的測試指令碼。

在這裡,我們使用selenium對我們的爬蟲進行輔助

環境 Ubuntu18.04
Chrome 本 67.0.3396.62(正式版本)
Firefox 60.0.1 (64 位)

selenium可以使用程式碼,對瀏覽器進行一個自動化的操作。但是既然要使用程式碼進行操作,那他一定需要異構驅動器。
首先我們來看他的一些部分程式碼

from selenium import webdriver

lagou_url = "https://www.lagou.com/zhaopin/xinmeitiyunying/?labelWords=label"
baidu_url = "https://www.baidu.com/"
browser = webdriver.Firefox() browser = webdriver.Chrome() browser = webdriver.Android() browser = webdriver.PhantomJS() browser.get(baidu_url) print(browser.page_source) browser.close()

其中browser定義了多種瀏覽器,selenium支援多種瀏覽器。但是不同的瀏覽器他所需要的驅動器不一樣
請看下面這張表格
下載chrome瀏覽器驅動地址:http://chromedriver.storage.googleapis.com/index.html


下載firefox瀏覽器驅動地址: https://github.com/mozilla/geckodriver/releases/
chrome版本對應: https://chromedriver.storage.googleapis.com/
常用的兩個,其他的自行搜尋
下載好之後,我們有兩種使用的方法
1.將驅動放置在python3同目錄之下,在ubuntu下是放置在/usr/bin下面
2.在browser = webdriver.Chrome()下直接寫入你驅動器的路徑

該錯誤表示你沒有裝驅動器,這裡我用phantomjs做舉例

/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/bin/python3 /home/alpaca/hello/test.py
/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/phantomjs/webdriver.py:49: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
  warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
Traceback (most recent call last):
  File "/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'phantomjs': 'phantomjs'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alpaca/hello/test.py", line 7, in <module>
    browser = webdriver.PhantomJS()
  File "/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 56, in __init__
    self.service.start()
  File "/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'phantomjs' executable needs to be in PATH. 

測試了chrome 在ubuntu下出現了

/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/bin/python3 /home/alpaca/hello/test.py
Traceback (most recent call last):
  File "/home/alpaca/hello/test.py", line 7, in <module>
    browser = webdriver.Chrome()
  File "/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
    self.service.start()
  File "/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 98, in start
    self.assert_process_still_running()
  File "/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
    % (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127

這一塊錯誤,但是找了很多帖子,也沒有辦法解決,所以我嘗試了firefox

Firfox測試成功