前提
市販のテキストをもとに、ウィキペディアのトップページのスクレイピングを勉強しています。
ですが、コード「entries = today.find_all("li")」で「AttributeError: 'str' object has no attribute 'find_all'」文字列にはfind_allは使えません?という表示が出てきてしまいました。
テキスト上では、記事にそれぞれ数字インデックスが付与される予定なのですが、その前の段階でつまずいてしましました。
アドバイスや見逃している点など、ご教授いただけますと幸いです。
実現したいこと
entries = today.find_all("li")
で、entriesにliで囲まれている文字列を抽出したいです。
発生している問題・エラーメッセージ
AttributeError Traceback (most recent call last)
Cell In [12], line 9
7 today = soup.find("div",attrs={"id":"on_this_day"}).text
8 print(today)
----> 9 entries = today.find_all("li")
10 print(entries)
11 today_list = []
AttributeError: 'str' object has no attribute 'find_all'
該当ソースコード
python
1import requests 2from bs4 import BeautifulSoup 3url = "https://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8" 4response = requests.get(url) 5soup = BeautifulSoup(response.content,"html.parser") 6today = soup.find("div",attrs={"id":"on_this_day"}).text 7print(today) 8entries = today.find_all("li") 9print(entries) 10today_list = [] 11index = 1 12 13for entry in entries: 14 today_list.append([index,entry.get_text()]) 15 index += 1 16print(today_list)
試したこと
entries = today.find_all("li")で、todayが文字列なのがいけないのかなと思い、変換してみましたが、失敗してしましました。申し訳ないです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/09/12 13:12