Parent article: Making an umbrella reminder with Raspberry Pi Zero W
As the title says. I'm new to Python and don't understand XML yet. I will study one by one. The version of Python I'm using is 2.7.9.
http://www.drk7.jp/weather/ It seems that the weather forecast information released by the Japan Meteorological Agency is distributed in XML format. Thank you for using it.
https://docs.python.jp/2.7/library/xml.etree.elementtree.html How to parse XML.
Displays the probability of precipitation in eastern Yokohama. I want to know mainly the probability of precipitation in the evening and at night, so
--12:00 --18:00 --18:00 --24:00
It is divided into two parts. I feel that there seems to be a more efficient way. Please teach me.
2017/08/13 Addendum: In the comment section, there is a cleaner code using the inclusion notation that @shiracamus taught me.
code
# coding: utf_8
import datetime
today = datetime.datetime.today().strftime("%Y/%m/%d")
import requests
url = 'http://www.drk7.jp/weather/xml/14.xml'
response = requests.get(url)
import xml.etree.ElementTree as ET
root = ET.fromstring(response.content)
# Get rainfallchance of North Yokohama
# Time: 12h - 18h and 18h - 24h
for area in root.iter('area'):
if area.get('id').encode('utf_8') == 'Eastern':
for info in area.findall('info'):
if info.get('date') == today:
rainfallchance = info.find('rainfallchance')
for period in rainfallchance.findall('period'):
hour = period.get('hour')
if hour == '12-18' or hour == '18-24':
print hour + 'h ' + period.text + '%'
result
12-18h 10%
18-24h 20%
Recommended Posts