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

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

新規登録して質問してみよう
ただいま回答率
85.48%
スクレイピング

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

Python

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

Q&A

1回答

908閲覧

python,seleniumでブラウザ操作、検索できない

kkmoto

総合スコア3

スクレイピング

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

Python

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

0グッド

0クリップ

投稿2022/09/23 14:55

編集2022/09/23 15:25

前提

pythonからseleniumでブラウザー操作をしたいと思っています。
Chromeは起動する

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

価格コムのページの検索まで進めません。

エラーメッセージ DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = webdriver.Chrome(executable_path=path ,options=options) InvalidArgumentException Traceback (most recent call last) Input In [8], in <cell line: 14>() 11 url = "https://kakaku.com/" 12 keyword = "iphone"#検索するキーワードを宣言 ---> 14 driver = webdriver.Chrome(executable_path=path ,options=options) 15 driver.get("url") 16 time.sleep(3)#3秒待つ File ~\anaconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py:69, in WebDriver.__init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, service, keep_alive) 66 if not service: 67 service = Service(executable_path, port, service_args, service_log_path) ---> 69 super().__init__(DesiredCapabilities.CHROME['browserName'], "goog", 70 port, options, 71 service_args, desired_capabilities, 72 service_log_path, service, keep_alive) File ~\anaconda3\lib\site-packages\selenium\webdriver\chromium\webdriver.py:92, in ChromiumDriver.__init__(self, browser_name, vendor_prefix, port, options, service_args, desired_capabilities, service_log_path, service, keep_alive) 89 self.service.start() 91 try: ---> 92 super().__init__( 93 command_executor=ChromiumRemoteConnection( 94 remote_server_addr=self.service.service_url, 95 browser_name=browser_name, vendor_prefix=vendor_prefix, 96 keep_alive=keep_alive, ignore_proxy=_ignore_proxy), 97 options=options) 98 except Exception: 99 self.quit() File ~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py:270, in WebDriver.__init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector, options) 268 self._authenticator_id = None 269 self.start_client() --> 270 self.start_session(capabilities, browser_profile) File ~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py:363, in WebDriver.start_session(self, capabilities, browser_profile) 361 w3c_caps = _make_w3c_caps(capabilities) 362 parameters = {"capabilities": w3c_caps} --> 363 response = self.execute(Command.NEW_SESSION, parameters) 364 if 'sessionId' not in response: 365 response = response['value'] File ~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py:428, in WebDriver.execute(self, driver_command, params) 426 response = self.command_executor.execute(driver_command, params) 427 if response: --> 428 self.error_handler.check_response(response) 429 response['value'] = self._unwrap_value( 430 response.get('value', None)) 431 return response File ~\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py:243, in ErrorHandler.check_response(self, response) 241 alert_text = value['alert'].get('text') 242 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here --> 243 raise exception_class(message, screen, stacktrace) InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir Stacktrace: Backtrace: Ordinal0 [0x00EDDF13+2219795] Ordinal0 [0x00E72841+1779777] Ordinal0 [0x00D8423D+803389] Ordinal0 [0x00DA2CD4+928980] Ordinal0 [0x00D9F179+913785] Ordinal0 [0x00DD36B9+1128121] Ordinal0 [0x00DD331A+1127194] Ordinal0 [0x00DCE616+1107478] Ordinal0 [0x00DA7F89+950153] Ordinal0 [0x00DA8F56+954198] GetHandleVerifier [0x011D2CB2+3040210] GetHandleVerifier [0x011C2BB4+2974420] GetHandleVerifier [0x00F76A0A+565546] GetHandleVerifier [0x00F75680+560544] Ordinal0 [0x00E79A5C+1808988] Ordinal0 [0x00E7E3A8+1827752] Ordinal0 [0x00E7E495+1827989] Ordinal0 [0x00E880A4+1867940] BaseThreadInitThunk [0x75DA6739+25] RtlGetFullPathName_UEx [0x772C8FD2+1218] RtlGetFullPathName_UEx [0x772C8F9D+1165]

該当のソースコード

python

1from selenium import webdriver 2from selenium.webdriver.chrome.options import Options 3import time 4 5options = Options() 6options.add_argument('--user-data-dir=C:\\Users\\AAA\\AppData\\Local\\Google\\Chrome\\User Data') 7options.add_argument('--profile-directory=Profile 1') 8options.add_argument('--lang=en') 9path = r'C:\Users\AAA\selenium\chromedriver.exe' 10 11url = "https://kakaku.com/" 12keyword = "iphone" 13 14driver = webdriver.Chrome(executable_path=path ,options=options) 15driver.get("url") 16time.sleep(3) 17 18search = driver.find_element_by_name("q") 19search.send_keys(keyword) 20search.submit() 21 22time. sleep(5) 23driver. quit()

試したこと

urlを他のサイトに変えてみたがやはり動かなかった

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

jupyte rnotebookを使用しています

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

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

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

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

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

guest

回答1

0

「user data directory is already in use」で検索して最初に出てきたサイトのリンクを貼っておきます。

https://qiita.com/Hidenatsu/items/e43ba04b4b5f710784e6

投稿2022/09/23 15:44

TakaiY

総合スコア12765

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

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

kkmoto

2022/09/24 01:43 編集

コメントありがとうございます。私のコードを実行するとuser data directory is already in useという文言はでるのですが、このコードでは普段使ってるchromeと別のユーザーアカウントを指定しており、TakaiK様が張ってくれたリンクの記事を書いた執筆者のような煩わしさを感じておりません。(エラーの文言がいちいち出るというストレスはありますが、実際の使用には問題ありません)。chrome起動後の価格コムにいかず、また、検索もできないことが主たる問題となっています。 また、上記記事の内容を実行しようとしたら Input In [5] options = New-Object OpenQA.Selenium.Chrome.ChromeOptions ^ SyntaxError: invalid syntax というエラーメッセージが出てしまいました、こちらはコピペして実行しただけなのでなぜこのようなエラーが出てしまったのかわかりません
TakaiY

2022/09/24 08:03

お役にたてなくて残念です。 関係無いとのことですが、そのエラーが出ないようにして動くようにしてみる必要はあるんじゃないかと思います。 ちなみに、そのエラーは実行方法が間違えているために出ています。 提示されているコマンドをjupyterで実行しているようですが、記事にもあるように、PowerShell上で実行するコマンドです。
kkmoto

2022/09/24 14:58

返信ありがとうございます。私はこのような記事を見つけました。 https://qiita.com/std-flower/items/ec59a46b7ad0e5d225ea 記事のコードを参考にjupyterをfirafoxで起動し、出力したらuser data directory is already in useというエラーが出ませんでした。しかし、firefox、Chromeを起動させながらjupyterを使えばやはり出ますね。 powerShellで実行してみます。 また、DeprecationWarningが邪魔なのでServiceオブジェクトでコードを書いてみたのですがその場合グーグルアカウントの指定はどのようにすればいいのでしょうか
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問