I have the following code which gets me close to where I want to be, but I want to access certain nested data within the period I have printed. The code:
import requests
from bs4 import BeautifulSoup
import datetime
today = str(datetime.date.today() + datetime.timedelta(days=1))
url = 'https://www.yr.no/place/Norway/Oslo/Oslo/Oslo/forecast.xml'
document = requests.get(url)
soup= BeautifulSoup(document.content, features='html.parser')
period = soup.find(attrs={'from':today + 'T12:00:00'})
print(period)
This gives me the following output (or similar to this basically as the days change):
<time from="2021-04-19T12:00:00" period="2" to="2021-04-19T18:00:00">
<!-- Valid from 2021-04-19T12:00:00 to 2021-04-19T18:00:00 -->
<symbol name="Clear sky" number="1" numberex="1" var="01d"></symbol>
<precipitation value="0"></precipitation>
<!-- Valid at 2021-04-19T12:00:00 -->
<winddirection code="WSW" deg="245.9" name="West-southwest"></winddirection>
<windspeed mps="1.8" name="Light breeze"></windspeed>
<temperature unit="celsius" value="14"></temperature>
<pressure unit="hPa" value="1028.4"></pressure>
</time>
I now want to drill down further to access the following:
- temperature value (eg. 14)
- temperature unit (eg. celsius)
- precipitation value (eg. 0)
I don't know how to drill down further (the reason for initially getting a particular date/period is that there are multiple on the XML, so need to first get the proper period, then drill down to the details of that period).