Unable to parse XML response and obtain elements
<p>This is my XML response from a <code>http</code> request </p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;Dataset name="aggregations/g/ds083.2/2/TP"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xml.opendap.org/ns/DAP2"
 xsi:schemaLocation="http://xml.opendap.org/ns/DAP2          
http://xml.opendap.org/dap/dap2.xsd" &gt;

    &lt;Attribute name="NC_GLOBAL" type="Container"&gt;
        &lt;Attribute name="Originating_or_generating_Center" type="String"&gt;
            &lt;value&gt;US National Weather Service, National Centres for Environmental Prediction (NCEP)&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Attribute name="Originating_or_generating_Subcenter" type="String"&gt;
            &lt;value&gt;0&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Attribute name="GRIB_table_version" type="String"&gt;
            &lt;value&gt;2,1&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Attribute name="Type_of_generating_process" type="String"&gt;
            &lt;value&gt;Forecast&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Attribute name="Analysis_or_forecast_generating_process_identifier_defined_by_originating_centre" type="String"&gt;
            &lt;value&gt;Analysis from GDAS (Global Data Assimilation System)&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Attribute name="file_format" type="String"&gt;
            &lt;value&gt;GRIB-2&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Attribute name="Conventions" type="String"&gt;
            &lt;value&gt;CF-1.6&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Attribute name="history" type="String"&gt;
            &lt;value&gt;Read using CDM IOSP GribCollection v3&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Attribute name="featureType" type="String"&gt;
            &lt;value&gt;GRID&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Attribute name="_CoordSysBuilder" type="String"&gt;
            &lt;value&gt;ucar.nc2.dataset.conv.CF1Convention&lt;/value&gt;
        &lt;/Attribute&gt;
    &lt;/Attribute&gt;

    &lt;Array name="time1"&gt;
        &lt;Attribute name="units" type="String"&gt;
            &lt;value&gt;Hour since 2007-12-06T12:00:00Z&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Attribute name="standard_name" type="String"&gt;
            &lt;value&gt;time&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Attribute name="long_name" type="String"&gt;
            &lt;value&gt;GRIB forecast or observation time&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Attribute name="calendar" type="String"&gt;
            &lt;value&gt;proleptic_gregorian&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Attribute name="_CoordinateAxisType" type="String"&gt;
            &lt;value&gt;Time&lt;/value&gt;
        &lt;/Attribute&gt;
        &lt;Float64/&gt;
        &lt;dimension name="time1" size="10380"/&gt;
    &lt;/Array&gt;

&lt;/Dataset&gt;
</code></pre>

<p>I am trying to parse this XML content using Python 3.5</p>

<pre><code>from xml.etree import ElementTree

response = requests.get("http://rda.ucar.edu/thredds/dodsC/aggregations/g/ds083.2/2/TP.ddx?time1")

tree = ElementTree.fromstring(response.content)

attr = tree.find("Attribute")
print(attr)
</code></pre>

<p>When I print this I get a <code>None</code>. What am I doing wrong? I also want to access the "Array" tag but that also returns <code>None</code>.</p>

<p>As stated in <a href="https://docs.python.org/2/library/xml.etree.elementtree.html#parsing-xml-with-namespaces" rel="nofollow">the doc</a>, due to the <code>xmlns="http://xml.opendap.org/ns/DAP2"</code>attribute of the Dataset root tag, all tag names you're looking for have to be prefixed by <code>{http://xml.opendap.org/ns/DAP2}</code>.</p>

<pre><code># should find something
tree.find("{http://xml.opendap.org/ns/DAP2}Attribute")
</code></pre>

<p>Reading this section of the ElementTree doc will also show you how to make something more readable with a dictionnary of namespaces.</p>

python xml python-3.x xml-parsing httprequest