Python 尝试直接从URL解析XML

Python 尝试直接从URL解析XML,python,python-3.x,Python,Python 3.x,我在看这个XML: 我想我可以使用下面的代码从列表中解析出'ethcty'和'cnt'项,但实际上我什么都没有得到 import xml.etree.ElementTree as ET tree = ET.parse('https://data.cityofnewyork.us/api/views/25th-nujf/rows.xml') root = tree.getroot() for child in root: print(child.tag, child.attrib)

我在看这个XML:

我想我可以使用下面的代码从列表中解析出'ethcty'和'cnt'项,但实际上我什么都没有得到

import xml.etree.ElementTree as ET
tree = ET.parse('https://data.cityofnewyork.us/api/views/25th-nujf/rows.xml')
root = tree.getroot()

for child in root:
    print(child.tag, child.attrib)

for _id in root.findall('_id'):
    rank = _id.find('ethcty').text
    name = _id.get('cnt')
    print(name, rank)
下面是URL中的示例


响应
元素中有一个
元素,因此您的
for
循环应该在
根[0]
中,而不是

下面是您的代码片段中的一个示例,希望它能帮助您理解这个问题

import xml.etree.ElementTree as ET
tree = ET.parse('rows.xml')
root = tree.getroot()

for _id in root[0].findall('row'):
    rank = _id.find('ethcty').text
    name = _id.find('cnt').text
    print(name, rank)
另外,
findall
应该是所需节点的名称

至于直接从url加载,您应该使用
urllib
,如下所示:

from urllib.request import urlopen
import xml.etree.ElementTree as ET

with urlopen('https://data.cityofnewyork.us/api/views/25th-nujf/rows.xml') as f:
    tree = ET.parse(f)
    root = tree.getroot()

    for _id in root[0].findall('row'):
        rank = _id.find('ethcty').text
        name = _id.find('cnt').text
        print(name, rank)

我编辑了后一段代码,因为我忘记了从您问题的URL部分加载,对此我感到抱歉

我很高兴能提供帮助!