Download and deserialize XML

The previous script downloaded the current weather from OpenWeatherMap in JSON format. This script downloads the current weather in XML format. First, however, you have to install lxml (lowercase LXML). In the Microsoft Windows command prompt window, you have to say py.exe -m pip instead of pip3.

pip3 search lxml
lxml (4.4.1)                   - Powerful and Pythonic XML processing library
                                 combining libxml2/libxslt with the
                                 ElementTree API.

pip3 install lxml
pip3 list
pip3 show lxml

xml.py

<?xml version="1.0" encoding="UTF-8"?>
<current><city id="5128581" name="New York"><coord lon="-74.02" lat="40.69"></coord><country>US</country><timezone>-14400</timezone><sun rise="2019-10-04T10:55:14" set="2019-10-04T22:34:16"></sun></city><temperature value="58.89" min="55.99" max="62.01" unit="fahrenheit"></temperature><humidity value="87" unit="%"></humidity><pressure value="1016" unit="hPa"></pressure><wind><speed value="8.5" unit="mph" name="Gentle Breeze"></speed><gusts></gusts><direction value="323.033" code="NW" name="Northwest"></direction></wind><clouds value="1" name="clear sky"></clouds><visibility value="16093"></visibility><precipitation mode="no"></precipitation><weather number="741" value="fog" icon="50d"></weather><lastupdate value="2019-10-04T12:49:33"></lastupdate></current>

<current>
  <city id="5128581" name="New York">
    <coord lon="-74.02" lat="40.69"/>
    <country>US</country>
    <timezone>-14400</timezone>
    <sun rise="2019-10-04T10:55:14" set="2019-10-04T22:34:16"/>
  </city>
  <temperature value="58.89" min="55.99" max="62.01" unit="fahrenheit"/>
  <humidity value="87" unit="%"/>
  <pressure value="1016" unit="hPa"/>
  <wind>
    <speed value="8.5" unit="mph" name="Gentle Breeze"/>
    <gusts/>
    <direction value="323.033" code="NW" name="Northwest"/>
  </wind>
  <clouds value="1" name="clear sky"/>
  <visibility value="16093"/>
  <precipitation mode="no"/>
  <weather number="741" value="fog" icon="50d"/>
  <lastupdate value="2019-10-04T12:49:33"/>
</current>


The temperature is 58.89° fahrenheit.

Things to try

  1. Convert the lastupdate, sunrise, and sunset times from UTC to our local time.
    import datetime
    
    lastupdate = root.find("lastupdate")
    if lastupdate == None:
        print("Couldn't find lastupdate.", file = sys.stderr)
        sys.exit(1)
    
    value = lastupdate.get("value")
    if value == None:
        print("Couldn't find value of temperature.", file = sys.stderr)
        sys.exit(1)
    
    print(value)
    localdateAndTime = datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S")
    localDateAndTime = localtime.replace(tzinfo = datetime.timezone.utc).astimezone(tz = None)
    print(f"localDateAndTime = {localDateAndTime}")
    print(localDateAndTime.strftime("%c"))
    print(localDateAndTime.strftime("%A, %B %-d, %Y %H:%M:%S (%I:%M:%S %p)"))
    

    Eastern Daylight Time is 4 hours after UTC:

    2019-10-04T12:49:33
    localDateAndTime = 2019-10-04 08:49:33-04:00
    Fri Oct  4 08:49:33 2019
    Friday, October 4, 2019 08:49:33 (08:49:33 AM)
    
  2. Display the icon, in this case
    http://openweathermap.org/img/wn/50d@2x.png
  3. The above lxml is third-party software from Germany (the .de in http://lxml.de/ is “Deutschland”), which we had to install with pip3. Its documentation boasts that it is “superior to the well-known ElementTree API”.

    To avoid third-party software, we could also have done the job with the following xml.etree.ElementTree and xml.dom.minidom modules of the Python Standard Library. But see the “billion laughs attack” in XML vulnerabilities.

    elementtree.py

    <?xml version="1.0" encoding="UTF-8"?>
    <current><city id="5128581" name="New York"><coord lon="-74.02" lat="40.69"></coord><country>US</country><timezone>-14400</timezone><sun rise="2019-10-04T10:55:14" set="2019-10-04T22:34:16"></sun></city><temperature value="60.03" min="55.99" max="64" unit="fahrenheit"></temperature><humidity value="82" unit="%"></humidity><pressure value="1017" unit="hPa"></pressure><wind><speed value="8.05" unit="mph" name="Gentle Breeze"></speed><gusts></gusts><direction value="290" code="WNW" name="West-northwest"></direction></wind><clouds value="1" name="clear sky"></clouds><visibility value="16093"></visibility><precipitation mode="no"></precipitation><weather number="741" value="fog" icon="50d"></weather><lastupdate value="2019-10-04T13:12:19"></lastupdate></current>
    
    <?xml version="1.0" ?>
    <current>
    	<city id="5128581" name="New York">
    		<coord lat="40.69" lon="-74.02"/>
    		<country>US</country>
    		<timezone>-14400</timezone>
    		<sun rise="2019-10-04T10:55:14" set="2019-10-04T22:34:16"/>
    	</city>
    	<temperature max="64" min="55.99" unit="fahrenheit" value="60.03"/>
    	<humidity unit="%" value="82"/>
    	<pressure unit="hPa" value="1017"/>
    	<wind>
    		<speed name="Gentle Breeze" unit="mph" value="8.05"/>
    		<gusts/>
    		<direction code="WNW" name="West-northwest" value="290"/>
    	</wind>
    	<clouds name="clear sky" value="1"/>
    	<visibility value="16093"/>
    	<precipitation mode="no"/>
    	<weather icon="50d" number="741" value="fog"/>
    	<lastupdate value="2019-10-04T13:12:19"/>
    </current>
    
    
    The temperature is 60.03° fahrenheit.
    The temperature is 60.03° fahrenheit.