0

I'm having difficulty parsing an XML tree using xml.etree.ElementTree in Python. Basically, I'm making a request to an API that gives an XML response, and trying to extract the values of several elements in the tree.

This is what I've done so far with no success:

root = etree.fromstring(resp_arr[0])
walkscore = root.find('./walkscore')

Here is my XML tree:

<result>
    <status>1</status>
    <walkscore>95</walkscore>
    <description>walker's paradise</description>
    <updated>2009-12-25 03:40:16.006257</updated>
    <logo_url>https://cdn.walk.sc/images/api-logo.png</logo_url>
    <more_info_icon>https://cdn.walk.sc/images/api-more-info.gif</more_info_icon>
    <ws_link>http://www.walkscore.com/score/1119-8th-Avenue-Seattle-WA-98101/lat=47.6085/lng=-122.3295/?utm_source=myrealtysite.com&utm_medium=ws_api&utm_campaign=ws_api</ws_link>
    <help_link>https://www.redfin.com/how-walk-score-works</help_link>
    <snapped_lat>47.6085</snapped_lat>
    <snapped_lon>-122.3295</snapped_lon>
</result>

Essentially, I'm trying to pull the walkscores from the XML document but my code isn't returning a value. Does anyone with experience using ElementTree have any advice to help me extract the values I'm after?

Sam

1 Answer 1

2

Your XML appears to be malformed. But if I replace instances of & with &amp;, then it's parseable:

>>> from xml.etree import ElementTree as ET
>>> tree = ET.fromstring(xml)
>>> tree.find('./walkscore').text
'95'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.