It's close to my own memo, but I tried it so I wrote an article. Originally I did a lot of research to do rasp pie signage, I didn't have the information I wanted to pinpoint, so I ended up investigating only the scraping method and managed to do it myself. (I wasn't a Python user or a person in the web area, so I struggled for a moment ...) Yahoo has a weather api, but I didn't use it because I only know the amount of precipitation.
C # edition https://qiita.com/MonoShobel/items/3ae0c051d2aadba698a4 There is.
There are some good and bad explanations about scraping itself, so you should read it.
By the way, it's Yahoo weather, but maybe scraping isn't prohibited. Yahoo Finance was explicitly banned, but I couldn't find any mention of the ban on the weather. Just keep moderation. Even if you scrape regularly, I think that access every hour or 30 minutes is enough for the weather.
tenki.py
import urllib3
from bs4 import BeautifulSoup
#URL to access
url = 'https://weather.yahoo.co.jp/weather/jp/13/4410.html'
#Access the URL In the return value, the instance containing the access result and HTML etc. will be returned.
http = urllib3.PoolManager()
instance = http.request('GET', url)
#Extract HTML from instance and parse it for beautiful Soup
soup = BeautifulSoup(instance.data, 'html.parser')
#Get the weather text with CSS selectors.
#today's weather
tenki_today = soup.select_one('#main > div.forecastCity > table > tr > td > div > p.pict')
print ("How is the weather today"+tenki_today.text)
#Tomorrow's weather
tenki_tomorrow = soup.select_one('#main > div.forecastCity > table > tr > td + td > div > p.pict')
print ("Tomorrow's weather"+tenki_tomorrow.text)
#Wait for input so that the screen does not disappear in an instant
input()
Below, the execution result
Recommended Posts