1,安裝selenium模塊
pip3 install selenium
2,安裝谷歌瀏覽器
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm -y
3
安裝chromedriver
1)運行下面命令查看瀏覽器版本
google-chrome --version
出現這個代表谷歌瀏覽器安裝成功
2)谷歌瀏覽器版本最新版124及以後得版本在這裏下載
https://googlechromelabs.github.io/chrome-for-testing/#canary
本地下載好解壓之後放到服務器任意位置即可,記得加上運行權限
chmod +x chromedriver
最後一步上代碼測試
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options # 指定驅動路徑 driver_path = '/mnt/chrome/chromedriver' # 創建ChromeOptions對象 chrome_options = Options() # 添加Chrome啓動參數 chrome_options.add_argument("--headless") #這兩個選項務必加上 chrome_options.add_argument('--no-sandbox') # 創建Service對象並傳入ChromeOptions service = Service(driver_path) # 創建WebDriver對象 driver = webdriver.Chrome(service=service, options=chrome_options) # 打開網頁 driver.get('https://www.baidu.com/') # 輸出網頁源代碼 print(driver.page_source) # 關閉瀏覽器 driver.quit()