猿问

尝试直接从 URL 解析 XML

我在看这个 XML:


https://data.cityofnewyork.us/api/views/25th-nujf/rows.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 中的示例。


https://docs.python.org/3.4/library/xml.etree.elementtree.html


慕码人2483693
浏览 166回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答