0

前提

使用環境
AWS lambda python3

seleniumを用いて位置情報を設定してスクレイピングするプログラムを作成しています.

実現したいこと

  • AWS lambda python3 の環境でselenium + headless chromium + chromedriverを用いてchromeに位置情報をsetしてスクレイピングしたい.

発生している問題・エラーメッセージ

実行環境①
python:
AWS lambda python3.7
selenium:
selenium 4.8.0
headless chrome :
https://github.com/adieuadieu/serverless-chrome/releases/
chromium 86.0.4240.111 (stable channel) for Amazon Linux 2
chromedriver:
https://chromedriver.storage.googleapis.com/index.html?path=86.0.4240.22/
chromedriver_linux64.zip

from selenium import webdriver
from selenium.webdriver.common.by import By


def chrome_int():
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument("--window-size=1920x1080")
    options.add_argument("--single-process")
    
    # user agent指定
    UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36'
    options.add_argument('--user-agent=' + UA)
    options.binary_location = "/opt/headless/headless-chromium"

    driver= webdriver.Chrome(
        # chromedriverのパスを指定
        executable_path="/opt/headless/chromedriver",
        options = options
    )

    driver = geo_setting(driver)

    return driver
    
def geo_setting(driver):
    # 位置情報の設定を許可(許可をしないと位置情報の設定ができない)
    driver.execute_cdp_cmd(
        "Browser.grantPermissions",
        {
            "permissions": ["geolocation"]
        },
    )
    # 緯度、経度、緯度・経度の誤差(単位:m)を設定する
    # 東京タワーの座標 
    latitude_ = 35.65856
    longitude_ = 139.745461
    driver.execute_cdp_cmd(
        "Emulation.setGeolocationOverride",
        {
            "latitude": latitude_,
            "longitude": longitude_,
            "accuracy": 100,
        },
    )
    return driver
 

time out (600sでもダメ) 以下で詰まっているポイ

driver= webdriver.Chrome(
       # chromedriverのパスを指定
       executable_path="/opt/headless/chromedriver",
       options = options
   )

実行環境②
python:
AWS lambda python3.7
selenium:
selenium 4.8.0
headless chrome :
https://github.com/adieuadieu/serverless-chrome/releases/
chromium 69.0.3497.81 (stable channel) for amazonlinux:2017.03
chromedriver:
https://chromedriver.storage.googleapis.com/2.43/chromedriver_linux64.zip

from selenium import webdriver
from selenium.webdriver.common.by import By


def chrome_int():
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument("--window-size=1920x1080")
    options.add_argument("--single-process")
    
    # user agent指定
    UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36'
    options.add_argument('--user-agent=' + UA)
    options.binary_location = "/opt/headless/headless-chromium"

    driver= webdriver.Chrome(
        # chromedriverのパスを指定
        executable_path="/opt/headless/chromedriver",
        options = options
    )

    driver = geo_setting(driver)

    return driver
    
def geo_setting(driver):
    # 位置情報の設定を許可(許可をしないと位置情報の設定ができない)
    driver.execute_cdp_cmd(
        "Browser.grantPermissions",
        {
            "permissions": ["geolocation"]
        },
    )
    # 緯度、経度、緯度・経度の誤差(単位:m)を設定する
    # 東京タワーの座標 
    latitude_ = 35.65856
    longitude_ = 139.745461
    driver.execute_cdp_cmd(
        "Emulation.setGeolocationOverride",
        {
            "latitude": latitude_,
            "longitude": longitude_,
            "accuracy": 100,
        },
    )
    return driver
 

エラーメッセージ

File "/var/task/lambda_function.py", line 31, in chrome_int
driver = geo_setting(driver)
File "/var/task/lambda_function.py", line 40, in geo_setting
"permissions": ["geolocation"]
File "/opt/python/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 145, in execute_cdp_cmd
return self.execute("executeCdpCommand", {'cmd': cmd, 'params': cmd_args})['value']
File "/opt/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/opt/python/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: unhandled inspector error: {"code":-32601,"message":"'Browser.grantPermissions' wasn't found"}
(Session info: headless chrome=69.0.3497.81)
(Driver info: chromedriver=2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d),platform=Linux 4.14.255-296-236.539.amzn2.x86_64 x86_64)

試したこと

    options.add_argument(f"--latitude={latitude_}")
    options.add_argument(f"--longitude={longitude_}")
    options.add_argument('--disable-popup-blocking')
    options.add_argument("--disable-notifications")

も試したが,エラーはできないものの,位置情報が反映されない
(https://www.gps-coordinates.net/my-location)

補足情報(FW/ツールのバージョンなど)

参考にしたサイト
layerの作成方法
https://dev.classmethod.jp/articles/aws-lambda-python-selenium-make-env/
位置情報の設定
https://qiita.com/ba--shi/items/3e766854f48b27d5114f

どうすれば,AWSで位置情報を設定したchromeでselenium スクレイピングできるのでしょうか⁇
よろしくお願いいたします

2

0

この質問に回答するには、ログインする必要があります。

のタグが付いた他の質問を参照する。