1. 程式人生 > 其它 >Python之selenium環境設定(linux版[centos])

Python之selenium環境設定(linux版[centos])

剛剛看到上一篇寫mac的記錄差不多兩年了,兩年的時間,雖然學的東西不少,感覺進步還是慢了寫。

這次為了前面一篇的獲取頭資訊或者cookies,需要在我的centos的linux機器上部署環境。

首先,需要在伺服器上安裝chrome瀏覽器的軟體

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

  

通過wget的命令,下載chrome的rpm版本,這個是從官網下載了的地址

這個地址,是我從網上查詢到的一個百度網盤的可用歷史版本的chrome瀏覽器比較全的安裝包大全。

百度網盤(含最新及歷史版本):

https://pan.baidu.com/s/195fycw8BEK4B1xhi4ig1Sg密碼:oig4

下載完成後,直接通過yum命令安裝。

yum install -y google-chrome-stable_current_x86_64.rpm

  

安裝完成後,可以通過指定路徑檢視chrome的版本資訊

/opt/google/chrome/chrome --version
Google Chrome 92.0.4515.107 unknown

  

安裝好chrome之後,我們可以更具chrome的版本資訊,去網站下載chromedriver外掛

網址https://chromedriver.storage.googleapis.com/index.html

通過wget下載指定的chromedriver之後,需要通過unzip解壓該檔案

wget https://chromedriver.storage.googleapis.com/92.0.4515.43/chromedriver_linux64.zip

  

解壓之後,可以看到chromedriver的可執行檔案,這裡其實已經完成了環境的安裝。

後續如果有需要,可以將chromedriver複製到系統的環境中。比較簡單的是通過echo $PATH檢視系統的環境路徑。

我就是將chromedriver複製到了系統環境最後一個目錄中,這樣就可以通過chromedriver --version來看chromedriver的版本資訊。

chromedriver --version
ChromeDriver 92.0.4515.43 (8c61b7e2989f2990d42f859cac71319137787cce-refs/branch-heads/4515@{#306})

  

最後pip install selenium,這個就不說了,easy。

這段測試程式碼,直接複製貼上,如果執行輸出結果,說明環境部署成功。

#!/usr/bin/env python
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument('--headless') # chrome_options.add_argument('--disable-gpu') # chrome_options.add_argument('--no-sandbox') # root使用者不加這條會無法執行 driver = webdriver.Chrome(options=chrome_options) for i in range(10): driver.get("https://www.baidu.com/") print(driver.title) driver.close()