Лучший способ приблизиться к этому, я считаю, сделать это следующим образом:
import requests
import pandas as pd
from lxml import etree
url = "https://www.yr.no/place/United_States/Massachusetts/Boston/forecast_hour_by_hour.xml"
req = requests.get(url) #use requests to get the data
doc = etree.XML(req.content) #since the data is in xml format, use an xml parser
#get location data using xpath:
loc = doc.xpath('//location/name/text()')[0]
lat = doc.xpath('//location/@latitude')[0]
lon = doc.xpath('//location/@longitude')[0]
alt = doc.xpath('//location/@altitude')[0]
rows = [] #initialize a list to collect the tabular data:
for dat in doc.xpath('//tabular'):
hours = dat.xpath('./time')
sky = dat.xpath('.//symbol')
tmpr = dat.xpath('.//temperature')
for t,s,tm in zip(hours,sky,tmpr):
rows.extend([(t.xpath('./@from')[0],t.xpath('./@to')[0],s.xpath('./@name')[0],tm.xpath('./@value')[0])])
#now prepare your dataframe:
columns = ['From','To','Sky','Temperature'] #set the column headers
print('Location: ',loc)
print('Longitude: ',lon)
print('Latitude: ',lat)
print('Altitude: ',alt)
#create the dataframe
pd.DataFrame(rows,columns=columns)
Вывод:
Location: Boston
Longitude: -71.05977
Latitude: 42.35843
Altitude: 14
From To Sky Temperature
0 2020-06-20T08:00:00 2020-06-20T09:00:00 Clear sky 24
1 2020-06-20T09:00:00 2020-06-20T10:00:00 Partly cloudy 27
2 2020-06-20T10:00:00 2020-06-20T11:00:00 Clear sky 29
et c.
Очевидно, вы можете изменить его, чтобы изменить отображаемые значения, форматирование и т. Д. c.