Python 尝试直接从URL解析XML
我在看这个XML: 我想我可以使用下面的代码从列表中解析出'ethcty'和'cnt'项,但实际上我什么都没有得到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)
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部分加载,对此我感到抱歉我很高兴能提供帮助!