服務器 配置ubuntu18.04 + selenium + firefox, 無界面讀取網頁

1. 服務器端安裝firefox

sudo apt-get install firefox

如安裝失敗,請先更新

sudo apt-get update
sudo apt-get upgrade

2. 安裝selenium

pip install selenium

3. 安裝geckodriver驅動

驅動地址:https://github.com/mozilla/geckodriver/

下載後解壓,上傳到服務器,然後移動到/usr/local/bin

sudo mv geckodriver /usr/local/bin

4. 測試安裝是否完成

這裏直接使用無界面的方式,即不跳出瀏覽器

from selenium import webdriver
import time

url = 'https://www.baidu.com'

# 設置無界面模型
option = webdriver.FirefoxOptions()
option.add_argument('--headless')

driver = webdriver.Firefox(firefox_options=option, executable_path='/usr/local/bin/geckodriver')
driver.get(url)

print(driver.title)

5. 報錯處理

 'geckodriver' executable may have wrong permissions

由權限問題導致,需提升geckodriver權限

sudo chmod +x /usr/local/bin/geckodriver

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章