pythonでseleniumを使ったweb情報の取得の勉強をしています。
(学習2日目)
環境
- windows10
- jupyter notebook
- python3.6.3(Anaconda)
例えばクックパッドのとあるページのレシピタイトルを取得・表示したい場合、下記のようにコードを書きました。
すると、'WebElement' object is not iterableと言うエラーがでてしまいます。
私のコードのどう修正したら良いのかご教授お願いできませんでしょうか。
python
1from selenium import webdriver 2 3#特定のページ(例としてクックパッド)を表示する。 4driver = webdriver.Chrome({Webドライバの場所}) 5URL = "https://cookpad.com/category/11" 6driver.get(URL) 7 8#レシピタイトルの要素を取得する。 9recipes = driver.find_element_by_class_name("recipe-title") 10 11#ページ内の同一要素を順番に取得して、表示させる。(←ここをどのように修正すべきか) 12for recipe in recipes: 13 print(recipes.text) 14
エラー文
TypeError Traceback (most recent call last)
<ipython-input-76-690d1824d8d1> in <module>()
1 recipes = driver.find_element_by_class_name("recipe-title")
2
----> 3 for recipe in recipes:
4 print(recipes.text)
TypeError: 'WebElement' object is not iterable
なお、上記のコードは以下のサイトを参考にしました。
【Python】Webスクレイピングチュートリアル -「次へ」ボタンが存在するページをすべて取得する場合 -
python
1posts = browser.find_elements_by_css_selector(".search-result") 2 3 for post in posts:#←ここでfor文を使っている 4 title = post.find_element_by_css_selector("h3").text 5 date = post.find_element_by_css_selector(".created").text 6 bookmarks = post.find_element_by_css_selector(".users span").text 7 se = pandas.Series([title, date, bookmarks],['title','date','bookmarks']) 8 df = df.append(se, ignore_index=True) 9 print(df)
以上、よろしくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/22 02:40