Извлечение информации с помощью ElementTree - PullRequest
0 голосов
/ 20 июня 2020

XML равно https://samples.openweathermap.org/data/2.5/forecast?q=London, us & mode = xml & appid = 439d4b804bc8187953eb36d2a8c26a02 Я хотел бы извлечь на консоль только широту и долготу, дату, час, высоту, количество осадков, давление, скорость ветра, направление ветра так, чтобы окончательный результат выглядел так (обычно первые три вещи (почасовой прогноз), остальные в таблице):

    City: London
    
    Latitude,longitude: 51.509865/-0.118092
    
    Altitude: 11

 From 2017-03-03T21:00:00 to: 2017-03-04T00:00:00
    **Name**           **Value**
    Rainfall:      0, 
    Pressure:      1012.92
    Wind spead     4.26
    wind direction West-northwest

From 2017-03-03T21:00:00 to: 2017-03-04T00:00:00
  **Name**           **Value**
    Rainfall:        0, 
    Pressure:        1012.92
    Wind spead       4.26
    wind direction   West-northwest
    
From...
.
.
.
e.t.c.

До сих пор только я писал это:

Скрипт:

import xml.etree.ElementTree as ET
import requests

def get_forecast_data(api_url):

    call = requests.get(api_url)
    call.raise_for_status()

    tree = ET.fromstring(call.text)
    for child in tree:
        print(child.tag, child.attrib)

if __name__ == "__main__":
    forecast_url = str(r'https://samples.openweathermap.org/data/2.5/forecast?q=London,us&mode=xml&appid=439d4b804bc8187953eb36d2a8c26a02')
    print(get_forecast_data(forecast_url))

И я застрял с ним, может ли кто-нибудь мне помочь?

Ответы [ 2 ]

0 голосов
/ 20 июня 2020

Ниже

import requests
import xml.etree.ElementTree as ET

r = requests.get('https://www.yr.no/place/United_States/Massachusetts/Boston/forecast_hour_by_hour.xml')
if r.status_code == 200:
    root = ET.fromstring(r.text)
    loc = root.find('./location')
    print('Location: {}'.format(loc.find('./name').text))
    inner_loc = loc.find('./location')
    print('Longitude: {}'.format(inner_loc.attrib['longitude']))
    print('Latitude: {}'.format(inner_loc.attrib['latitude']))
    print('Altitude: {}'.format(inner_loc.attrib['altitude']))
    print()

    entries = root.findall('.//time')
    for e in entries:
      print('From: {} to: {}'.format(e.attrib['from'],e.attrib['to']))
      p = e.find('./precipitation')
      t = e.find('./temperature')
      c = e.find('./symbol')
      print('Cloudiness: {}, precipitation: {} , Temperature: {}'.format(c.attrib['name'],p.attrib['value'],t.attrib['value']))
      print()
 
else:
    print('Failed to read xml. status code: {}'.format(r.status_code))
0 голосов
/ 20 июня 2020

Лучший способ приблизиться к этому, я считаю, сделать это следующим образом:

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.

...