質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.31%
Google Cloud Platform

Google Cloud Platformは、Google社がクラウド上で提供しているサービス郡の総称です。エンドユーザー向けサービスと同様のインフラストラクチャーで運営されており、Webサイト開発から複雑なアプリ開発まで対応可能です。

スクレイピング

スクレイピングとは、公開されているWebサイトからページ内の情報を抽出する技術です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

Q&A

解決済

1回答

1379閲覧

Google Cloud FunctionsでSeleniumを動かしたい

kd133

総合スコア2

Google Cloud Platform

Google Cloud Platformは、Google社がクラウド上で提供しているサービス郡の総称です。エンドユーザー向けサービスと同様のインフラストラクチャーで運営されており、Webサイト開発から複雑なアプリ開発まで対応可能です。

スクレイピング

スクレイピングとは、公開されているWebサイトからページ内の情報を抽出する技術です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

0グッド

1クリップ

投稿2023/12/01 00:10

編集2023/12/04 01:23

実現したいこと

Cloud FunctionsでSeleniumを使ってWebスクレイピングをしたい。

前提

下記URLのコードをそのままデプロイし、テストを実行するとエラーが発生しました。
トリガーはCloud Pub/Subを使用したいのでエントリポイントのみmain(data,context):に変更しています。
ランタイムはPython3.12に設定しています。
環境は第1世代、メモリを1GB割り当てています。

https://qiita.com/NearMugi/items/8146306168dd6b41b217

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

テストのログです。
7~9行目のログに重大度のエラーマークがついています。

txt

1Function execution started 2driver setting 3copy headless-chromium 4copy chromedriver 5get driver 6ERROR in app: Exception on / [POST] 7testcsss0xm48uvg Traceback (most recent call last): 8 File "/layers/google.python.pip/pip/lib/python3.12/site-packages/flask/app.py", line 2529, in wsgi_app 9 response = self.full_dispatch_request() 10File "/layers/google.python.pip/pip/lib/python3.12/site-packages/flask/app.py", line 1825, in full_dispatch_request 11 rv = self.handle_user_exception(e) 12File "/layers/google.python.pip/pip/lib/python3.12/site-packages/flask/app.py", line 1823, in full_dispatch_request 13 rv = self.dispatch_request() 14 File "/layers/google.python.pip/pip/lib/python3.12/site-packages/flask/app.py", line 1799, in dispatch_request 15 return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) 16File "/layers/google.python.pip/pip/lib/python3.12/site-packages/functions_framework/__init__.py", line 171, in view_func 17 function(data, context) 18 File "/workspace/main.py", line 65, in main 19 settingDriver() 20 File "/workspace/main.py", line 60, in settingDriver 21 driver = webdriver.Chrome(executable_path=driverPath, options=chrome_options) 22 Function execution took 990 ms, finished with status: 'crash' 23 TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path'

該当のソースコード

python

1import os 2import shutil 3import stat 4from pathlib import Path 5 6from selenium import webdriver 7 8global driver 9 10 11def add_execute_permission(path: Path, target: str = "u"): 12 """Add `x` (`execute`) permission to specified targets.""" 13 mode_map = { 14 "u": stat.S_IXUSR, 15 "g": stat.S_IXGRP, 16 "o": stat.S_IXOTH, 17 "a": stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH, 18 } 19 20 mode = path.stat().st_mode 21 for t in target: 22 mode |= mode_map[t] 23 24 path.chmod(mode) 25 26 27def settingDriver(): 28 print("driver setting") 29 global driver 30 31 driverPath = "/tmp" + "/chromedriver" 32 headlessPath = "/tmp" + "/headless-chromium" 33 34 # copy and change permission 35 print("copy headless-chromium") 36 shutil.copyfile(os.getcwd() + "/headless-chromium", headlessPath) 37 add_execute_permission(Path(headlessPath), "ug") 38 39 print("copy chromedriver") 40 shutil.copyfile(os.getcwd() + "/chromedriver", driverPath) 41 add_execute_permission(Path(driverPath), "ug") 42 43 chrome_options = webdriver.ChromeOptions() 44 45 chrome_options.add_argument("--headless") 46 chrome_options.add_argument("--disable-gpu") 47 chrome_options.add_argument("--window-size=1280x1696") 48 chrome_options.add_argument("--no-sandbox") 49 chrome_options.add_argument("--hide-scrollbars") 50 chrome_options.add_argument("--enable-logging") 51 chrome_options.add_argument("--log-level=0") 52 chrome_options.add_argument("--v=99") 53 chrome_options.add_argument("--single-process") 54 chrome_options.add_argument("--ignore-certificate-errors") 55 chrome_options.add_argument("--disable-dev-shm-usage") 56 57 chrome_options.binary_location = headlessPath 58 59 print("get driver") 60 driver = webdriver.Chrome(executable_path=driverPath, options=chrome_options) 61 62 63def main(data,context): 64 settingDriver() 65 66 global driver 67 try: 68 print("URL get") 69 driver.get("http://[hoge]") 70 targetPath = '[HOGE]' 71 ret = driver.find_element_by_xpath(targetPath) 72 ret = ret.get_attribute("alt") 73 print(ret) 74 75 finally: 76 print("driver quit") 77 driver.quit() 78 79 return ret 80

txt

1google-cloud-error-reporting==0.30.0 2selenium 3setuptools

chromedriver、headless-chromiumファイルを含む

試したこと

  • 再デプロイ

変化なし

  • Seleniumのバージョンを指定「selenium==3.141.0」

変化なし

  • ランタイムをPython 3.8に変更

変化なし

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自己解決しました。
Seleniumのバージョンを「selenium==3.141.0」に指定するとエラー解消されました。
再デプロイするときに新しいコードが上書きされていませんでした。
cloud storageに保存されている古いコードを一度削除してから、新しいコードをデプロイしました。

投稿2023/12/14 00:31

kd133

総合スコア2

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.31%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問