Q&A
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- seleniumでブラウザ操作して、beautifulsoupで取得したグーグルニュースの情報をエクセルファイルに出力したい。
前提
ここに質問の内容を詳しく書いてください。
pythonでウェブスクレイピングを勉強しています。
ユーチューブ動画やサイトなどを調べて作ってみましたが、グーグルニュースをfirefoxで開いたあと、検索ボックスに調べたいワードが自動で入力されず、サイトが開くだけで止まってしまいます。
pc:thinkpad x395
os:Ubuntu 22.04.1 LTS
エディタ:visual studio code
発生している問題・エラーメッセージ
エラーメッセージ input_tag_name = driver.find_element(By.CLASS_NAME,'Ax4B8 ZAGvjd') File "/home/pengin/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 856, in find_element return self.execute(Command.FIND_ELEMENT, { File "/home/pengin/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 434, in execute self.error_handler.check_response(response) File "/home/pengin/.local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .Ax4B8 ZAGvjd Stacktrace: RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8 WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:180:5 NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:392:5 element.find/</<@chrome://remote/content/marionette/element.sys.mjs:133:16 ### 該当のソースコード ```ここに言語名を入力 python3 ソースコード import urllib.request from bs4 import BeautifulSoup import pandas as pd from urllib.parse import urljoin from selenium import webdriver import time from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys #キーボード操作のためのモジュール import datetime now = datetime.datetime.now() # 現在時刻の取得 today = now.strftime('%Y年%m月%d日') # 現在時刻を年月曜日で表示 #検索蘭にキーワードを記入 keys = input("検索キーワード:") seach_word = input('絞り込み') #Google Chromeのドライバを用意 driver = webdriver.Firefox() #Google mapsを開く news = "https://news.google.com/topics/CAAqJggKIiBDQkFTRWdvSUwyMHZNRFZxYUdjU0FtcGhHZ0pLVUNnQVAB?hl=ja&gl=JP&ceid=JP%3Aja" driver.get(news) time.sleep(5) input_tag_name = driver.find_element(By.CLASS_NAME,'Ax4B8 ZAGvjd') #データ入力 input_tag_name.send_keys(seach_word) input_tag_name.send_keys(seach_word) input_tag_name.send_keys(Keys.ENTER) #エンターキーを押す class Scraper: def __init__(self,site): self.site = site def scrape(self): r = urllib.request.urlopen(self.site) html = r.read() parser = "html.parser" soup = BeautifulSoup(html,parser) titals = [] urls = [] seach_url = 'https://news.google.com/search' for tag in soup.find_all(class_ = "VDXfz"): tital = tag.get("aria-label") url = tag.get("href") if seach_word in tital: titals.append(tital) url = urllib.parse.urljoin(seach_url,url) urls.append(url) df = pd.DataFrame() df [today] = titals df ['URL'] = urls df.to_csv('web.csv', mode = 'a',index=False) Scraper(news).scrape() ### 試したこと ここに問題に対して試したことを記載してください。 ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。