LoginSignup
18
16

More than 3 years have passed since last update.

[Python] Headless Chrome を AWS Lambda で動かす

Last updated at Posted at 2020-04-05
  1. serverless-chromeをダウンロードして解凍
  2. chromedriverをダウンロードして解凍

  3. 実行権限付与

    • chmod 777 <ダウンロードしたファイル>
  4. chrome フォルダにダウンロードしたファイルを入れて、zipに固める

  5. Lambdaレイヤーにzipをアップロード

  6. Lambdaレイヤーにseleniumをアップロード

  7. Lambda関数に上記2つのレイヤーを追加

  8. 以下のようなコードで実行できる

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def lambda_handler(event, context):
    options = Options()
    options.binary_location = '/opt/chrome/headless-chromium'
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--single-process')
    options.add_argument('--disable-dev-shm-usage')

    browser = webdriver.Chrome('/opt/chrome/chromedriver', chrome_options=options)
    browser.get('https://www.google.com')
    title = browser.title
    browser.close()
    browser.quit()

    return {"title": title}

参考

https://blog.ikedaosushi.com/entry/2018/12/22/231421
https://qiita.com/nabehide/items/754eb7b7e9fff9a1047d

環境

macOS 10.15.4
Python 3.7.7

18
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
16