111 字
1 分鐘
Heroku 中使用selenium 設定方法

介紹#

你會找到到這篇也許你有相同的問題,Heroku 使用 selenium 爬蟲非常簡單就可以解決問題,看看如何設定。

教學影片#

方法#

Heroku 設定套件 Buildpacks#

heroku/python

https://github.com/heroku/heroku-buildpack-google-chrome(在新分頁開啟)

https://github.com/heroku/heroku-buildpack-chromedriver(在新分頁開啟)

環境變數設定#

環境變數路徑
CHROMEDRIVER_PATH/app/.chromedriver/bin/chromedriver
GOOGLE_CHROME_BIN/app/.apt/usr/bin/google-chrome

例子:爬 google.com 並印出原始碼

import os
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
chrome_options.add_argument("--headless") #無頭模式
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=chrome_options)
driver.get("https://www.google.com")
print(driver.page_source)
Heroku 中使用selenium 設定方法
https://aishuafei.com/posts/heroku-中使用selenium-設定方法/
作者
愛耍廢
發佈於
2020-05-12
許可協議
CC BY-NC-SA 4.0