Qiitaを参考に、このサイト(https://tenki.jp/forecast/3/16/4410/13103/)をスクレイピングしよと思ったところ、このようなコードが使われていたのですがstringがどこから来ているのかわからないです。
python
1today = bsObj.find(class_="today-weather") 2weather = today.p.string
スクレイピングしたい部分を検証で見るとこんな感じです。
html
1<p class="weather-telop">晴</p>
追記
python
1import requests 2from bs4 import BeautifulSoup 3#目的のURL 4url = "https://tenki.jp/forecast/3/16/4410/13103/" 5 6#HTTPリクエスト 7r = requests.get(url) 8 9bsObj = BeautifulSoup(r.content,"html.parser") 10 11today = bsObj.find(class_="today-weather") 12weather = today.p.string 13 14#気温情報 15temp = today.div.find(class_="date-value-wrap") 16#<dd>タグを全て取り出す 17temp=temp.find_all("dd") 18#最高気温 19temp_max = temp[0].span.string 20#最高気温の前日比 21temp_max_diff = temp[1].string 22#最低気温 23temp_min = temp[2].span.string 24#最低気温の前日比 25temp_min_diff = temp[3].string 26 27print("天気:{}".format(weather)) 28print("最高気温:{}{}".format(temp_max,temp_max_diff)) 29print("最低気温:{}{}".format(temp_min,temp_min_diff)) 30
回答1件
あなたの回答
tips
プレビュー