Trying to parse XML directly from a URL

Viewed 2243

I'm looking at this XML:

https://data.cityofnewyork.us/api/views/25th-nujf/rows.xml

I thought I could parse out the 'ethcty' and 'cnt' items from the list, using the code below, but I'm actually getting nothing at all.

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)

I am following the examples from the URL below.

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

1 Answers

Inside the response element there's a row element, so your for loop should be in root[0] instead of root

Here's an example from your snippet, hope it helps you understand the issue

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)

Also, findall should be the name of the node you want

As for loading directly from the url you should use the urllib as follows:

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)

I edited the latter code because I forgot about the loading from the URL part of your question, i'm sorry about that

Related