Python Xml Parsing for Wolfram Api
Question: I'm trying to get the out put of the wolfram api using a python xml parsing
script. Here's my script:

    
    
    import urllib
    import urllib.request
    import xml.etree.ElementTree as ET
    
    xml_data=urllib.request.urlopen("http://api.wolframalpha.com/v2/query?input=sqrt+2&appid=APLTT9-9WG78GYE65").read()
    root = ET.fromstring(xml_data)
    
    for child in root:
       print (child.get("title"))
       print (child.attrib)
    

I know It's only getting the attributes of everything in the title portion of
the code but it's a start.

Here's a snippet of the output:

    
    
    <pod title="Input" scanner="Identity" id="Input" position="100" error="false" numsubpods="1">
     <subpod title="">
     <plaintext>sqrt(2)</plaintext>
    

I'm trying to get it to only print out what is in the tags. Does anyone know
how to edit the code to get that?


Answer: Only the `<plaintext>` elements contain text:

    
    
    for pt in root.findall('.//plaintext'):
        if pt.text:
            print(pt.text)
    

The `.text` attribute holds the text of an element.

For your URL, that prints:

    
    
    sqrt(2)
    1.4142135623730950488016887242096980785696718753769480...
    [1; 2^_]
    Pythagoras's constant
    sqrt(2)~~1.4142  (real, principal root)
    -sqrt(2)~~-1.4142  (real root)
    

It looks like the `<pod>` tags have interesting titles too:

    
    
    for pod in root.findall('.//pod'):
        print(pod.attrib['title'])
        for pt in pod.findall('.//plaintext'):
            if pt.text:
                print('-', pt.text)
    

which then prints:

    
    
    Input
    - sqrt(2)
    Decimal approximation
    - 1.4142135623730950488016887242096980785696718753769480...
    Number line
    Continued fraction
    - [1; 2^_]
    Constant name
    - Pythagoras's constant
    All 2nd roots of 2
    - sqrt(2)~~1.4142  (real, principal root)
    - -sqrt(2)~~-1.4142  (real root)
    Plot of all roots in the complex plane
    

