I needed a weather forecast for my own IoT system.
I used BeautifulSoup to get the time and probability of precipitation in Kusatsu from tenki.jp.
python requires beautifulsoup4
.
conda install beautifulsoup4 requests -y
Investigate the classes and tags of the related parts by verifying with Chrome. I wanted the probability of precipitation every hour, so I looked it up.
class = forecast-point-1h class = prob-precip There was data in the span.
Therefore, the extraction method is as follows.
scraping
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
#tenki.URL of the page of the target area of jp(Kusatsu City, Shiga Prefecture)
url = 'https://tenki.jp/forecast/3/16/4410/13208/'
#HTTP request
r = requests.get(url)
bsObj = BeautifulSoup(r.content, "html.parser")
kusatu_1h = bsObj.find(class_="forecast-point-1h")
##time
h = []
hour = kusatu_1h.find(class_="hour")
h_tem = hour.find_all('span')
h = [int(h_tem[x].string) for x in range(len(h_tem))]
print(h)
##rainy percent
kousui = kusatu_1h.find(class_="prob-precip")
k_tem = kousui.find_all('span')
k = [k_tem[x].string for x in range(len(k_tem))]
print(k)
Execution result
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
['(%)', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '---', '90', '90', '70', '70', '70', '80', '70']
Get today's weather and temperature by web scraping --Qiita
Recommended Posts