実現したいこと
初心者質問で申し訳ありません。
seleniumを使ってHPから情報を取得しようとしています。
https://incidecoder.com/
検索で出てきた商品のURLと商品名を取得したいのですが、エラーが出てしまいます。
ループ処理はとりあえず置いておいて、1箇所だけでも取得しようとしていますが、
FIND_PLACEのところでエラーと言われてしまいます。
詳しい方教えていただけますとありがたいです、よろしくお願いします。
発生している問題・分からないこと
###以下の指定の仕方が違うような気がしています。
エラーメッセージ
error
1Traceback (most recent call last): 2 File "/Users...", line 28, in <module> 3 FIND_PLACE = browser.find_element(By.XPATH, "/html/body/table") 4 File "/Users...", line 742, in find_element 5 return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"] 6 File "/Users...", line 348, in execute 7 self.error_handler.check_response(response) 8 File "/Users...", line 229, in check_response 9 raise exception_class(message, screen, stacktrace) 10selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/table"} 11 (Session info: chrome=120.0.6099.216); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception 12Stacktrace: 130 chromedriver 0x000000010ad75cc8 chromedriver + 4844744 14##略 15
該当のソースコード
取得したいのはhrefリンクとChanel Signature De Chanelのような商品名のテキスト部分です。
全体像は以下の通りです。
<html> <head> <meta name="console-scheme" content="light dark"> </head> <body> <div class="line-gutter-backdrop"></div> <form autocomplete="off"> <label class="line-wrap-control"></label> </form> <table> <tbody> <tr></tr> <tr></tr> 略 <tr> <td class="line-number" value="268"></td> <td class="line-content"> <span class="html-tag"> "<a " <span class="html-attribute-value html-external-link" target="blank" href="/products/chanel-signature-de-chanel" rel="noneferrer noopener"> /products/chanel-signature-de-chanel</a> "" " <span class="html-attribute-value">klavika simpletextlistitem</span> "">" </span> "Chanel Signature De Chanel"
該当箇所のdiv内は以下の通りです。
<div class="paddingbl"> <a href="/products/chanel-signature-de-chanel" class="klavika simpletextlistitem">Chanel Signature De Chanel</a> <a href="/products/chanel-ndeg1-de-chanel-revitalizing-serum" class="klavika simpletextlistitem">Chanel N°1 De Chanel Revitalizing Serum</a> ##略 <div class="home-create-box paddingt30"> <span> Can't find what you are looking for? <span class="search-bumper hideotablet"></span> <a href="/products/create"> <button> <img src="https://incidecoder-assets.storage.googleapis.com/assets/img/star_goodie.png" alt=""></img> UPLOAD IT </button> </a> </span> </div> </div>
python
1from selenium import webdriver 2from selenium.webdriver.common.by import By 3import time 4import tqdm 5import pandas as pd 6from selenium.webdriver.chrome.options import Options 7 8MIN_INTERVAL = 0.5 9INTERVAL = 2.5 10MINUTE_INTERVAL = 60 11 12options = Options() 13options.add_argument("--headless") 14 15browser = webdriver.Chrome(options=options) 16time.sleep(INTERVAL) 17browser.get("https://incidecoder.com") 18time.sleep(INTERVAL) 19 20browser.find_element(By.ID, "query").send_keys('chanel') 21browser.find_element(By.XPATH, "//div[1]/div[2]/div[1]/form/input[2]").click() 22input("つづける?") 23time.sleep(INTERVAL) 24 25### 26FIND_PLACE = browser.find_element(By.XPATH, "/html/body/table") 27Find_class = FIND_PLACE.find_elements(By.CLASS_NAME, "line-content") 28for i in Find_class: 29 print(i.text)
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
解決できませんでした。
補足
特になし

回答2件
あなたの回答
tips
プレビュー