Hi, my name is Yoshiya Kato from Quick Italate Co., Ltd. I'm currently writing a blog post titled "Why I recommend python to get started".
** Get the weather forecast of the office in XML, format it and display it. ** **
—— Find a service that provides enough information. --Investigate how to use the service. --Make a request. --Extract the necessary information from the acquired XML. --Display the extracted information neatly.
If you search for "weather forecast API free" I found that the following services existed. -Livedoor Weather Web Service
OpenWeatherMap is structured so that detailed information can be obtained in various ways. I will extract it and make it a table.
Parameters | meaning | Value example | Mandatory |
---|---|---|---|
URL | To send the request | http://api.openweathermap.org/data/2.5/forcast? | Mandatory |
appid | Assigned ID | Mandatory*1 | Mandatory |
name | City-specific name | tokyo-to | option*2 |
id | City-specific ID(cityid) | 1850144 | option*2 |
lat | Latitude in decimal notation | 35.78 | option*2 |
lon | Latitude in decimal notation | 139.83 | option*2 |
zip | Postal code | 1210053 | option*2 |
country | Country name abbreviation (when zip is specified) | jp | option*2 |
unites | Notation unit*1 | metric or imperial or none | option*2 |
mode | Specify acquisition format | xml or json or html | option*2 |
Image of request URL (when using Cityid)) |
---|
http://api.openweathermap.org/data/2.5/forecast?id=1850144&APPID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&units=metric&mode=xml |
Image of request URL (when using latitude / longitude)) |
---|
http://api.openweathermap.org/data/2.5/forecast?lon=139.83&lat=35.78&APPID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&units=metric&mode=xml |
Now that the information is ready, I would like to use python to get the information and display it in the form of formatted XML.
openweathermap.py
#Library import
import urllib.request
import urllib.parse
import xml.etree.ElementTree as et
import xml.dom.minidom as md
url='http://api.openweathermap.org/data/2.5/forecast?' #Base URL settings
query = {
'id' : '1850144' ,
'APPID' : 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', #The appid you got
'units' : 'metric',
'mode' : 'xml'}#Value group to be set in the query
url = url + urllib.parse.urlencode(query) #Generate request URL
response = urllib.request.urlopen(url) #http request
root = et.fromstring(response.read()) #Store the retrieved content in XML Element
Check the contents of the url used for the request.
Check the generated url
print(url) #Show the contents of the url
http://api.openweathermap.org/data/2.5/forecast?id=1850144&APPID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&units=metric&mode=xml
Code to display xml
document = md.parseString(et.tostring(root, 'utf-8')) #minidom module parses XML Element
print(document.toprettyxml(indent=" ")) #Converts parsed XML information into a string with indentation and displays it
This is the XML that is actually formatted and output from the value returned by the request.
result.xml
<?xml version="1.0" ?>
<weatherdata>
<location>
<name>Tōkyō-to</name>
<type/>
<country>JP</country>
<timezone/>
<location altitude="0" geobase="geonames" geobaseid="1850144" latitude="35.6895" longitude="139.6917"/>
</location>
<credit/>
<meta>
<lastupdate/>
<calctime>0.0129</calctime>
<nextupdate/>
</meta>
<sun rise="2017-06-23T19:26:28" set="2017-06-24T10:00:54"/>
<forecast>
<time from="2017-06-24T06:00:00" to="2017-06-24T09:00:00">
<symbol name="broken clouds" number="803" var="04d"/>
<precipitation/>
<windDirection code="SSE" deg="148.001" name="South-southeast"/>
<windSpeed mps="2.41" name="Light breeze"/>
<temperature max="24.68" min="23.88" unit="celsius" value="23.88"/>
<pressure unit="hPa" value="1015.57"/>
<humidity unit="%" value="66"/>
<clouds all="56" unit="%" value="broken clouds"/>
</time>
・ ・ (Omitted in the middle) ・ ・
</forecast>
</weatherdata>
Next time, I would like to extract the contents of XML and process it.
Thank you very much.