Python3 + selenium 擷取網站狀態快照
系統更新與軟體服務預裝
$ sudo apt-get update
安裝 Python3 pip
$ sudo apt-get install python3 python3-dev python3-pip
安裝 selenium
$ pip3 install selenium
安裝 chromium browser / driver
安裝 chromium browser
$ sudo apt-get install chromium-browser
安裝 chromium driver
下載 chromium driver Release
Ubuntu 16.04 / 18.04
$ wget http://launchpadlibrarian.net/469641508/chromium-chromedriver_80.0.3987.149-0ubuntu0.18.04.1_amd64.deb
Raspberry Pi
$ wget http://launchpadlibrarian.net/469843212/chromium-chromedriver_80.0.3987.149-0ubuntu0.18.04.1_arm64.deb
安裝 chromium driver 的 deb 包
Ubuntu 16.04 / 18.04 安裝
$ sudo dpkg -i chromium-chromedriver_80.0.3987.149-0ubuntu0.18.04.1_amd64.deb
Raspberry Pi 安裝
$ sudo dpkg -i chromium-chromedriver_80.0.3987.149-0ubuntu0.18.04.1_arm64.deb
測試網站抓取
程式碼區塊
Server 無螢幕顯示版本
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('window-size=1920x1080')
option.add_argument("--start-maximized")
option.add_argument('--headless')
driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver', options=option)
driver.get('https://www.spotify.com')
screenshot = driver.save_screenshot('my_screenshot.png')
driver.quit()
有螢幕版本
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument("--start-maximized")
driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver', options=option)
driver.get('https://www.spotify.com')
screenshot = driver.save_screenshot('my_screenshot.png')
driver.quit()