使用python循环处理复杂的xml数据以获得特定值

时间:2019-01-02 04:12:32

标签: python xml

我想获取temperature<element type = 'air_temperature_maximum'>中的值<forecast-period>。我只想要'Melbourne'区域。

xml来源= http://www.bom.gov.au/fwo/IDV10753.xml

我尝试了以下操作,但这只会打印出整个解析的xml,而不是我打算得到的。

import xml.etree.ElementTree as ET
import requests

url = "http://www.bom.gov.au/fwo/IDV10753.xml"
response = requests.get(url, verify=False).content.decode('UTF-8')

tree = ET.parse(response)
print(tree.find('product').find('amoc').find('forecast').find('area')
.find('forecast-period').find('element').text)

我想要<element type = 'air_temperature_maximum'>区域中'Melbourne'中所有的7天温度值。任何帮助深表感谢。

1 个答案:

答案 0 :(得分:2)

您可以通过暴力在多个嵌套循环中遍历XML:

from xml.etree.ElementTree import fromstring, ElementTree
from requests import get

url = 'http://www.bom.gov.au/fwo/IDV10753.xml'

req = get(url)
tree = ElementTree(fromstring(req.text))
root = tree.getroot()

for outer in root:
    if outer.tag == 'forecast':
        for inner in outer:
            if inner.attrib['description'] == 'Melbourne':
                for element in inner:
                    for temp in element:
                        if temp.attrib["type"] == 'air_temperature_maximum':
                            print(temp.text)

哪个给出7个温度:

23
28
42
24
22
24
27

您还可以使用列表推导功能将温度存储在列表中:

for outer in root:
    if outer.tag == "forecast":
        for inner in outer:
            if inner.attrib["description"] == "Melbourne":
                temps = [
                    temp.text
                    for element in inner
                    for temp in element
                    if temp.attrib["type"] == "air_temperature_maximum"
                ]
                print(temps)

温度列表:

['23', '28', '42', '24', '22', '24', '27']

我将这些温度的最终转换留给您。