16
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

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

  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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
16
Help us understand the problem. What are the problem?