Trouble parsing XML with python
<p>I have parsed an XML file with BeautifulSoup in Python and I am having trouble extracting the data out of it. An example of the structure of the XML is below:</p>

<pre><code>&lt;Products page="0" pages="-1" records="27"&gt;
  &lt;Product id="ABC001"&gt;
    &lt;Name&gt;This product name&lt;/Name&gt;
    &lt;Cur&gt;USD&lt;/Cur&gt;
    &lt;Tag&gt;Text&lt;/Tag&gt;
    &lt;Classes&gt;
      &lt;Class id="USD"&gt;
        &lt;ClassCur&gt;USD&lt;/ClassCur&gt;
        &lt;Identifier&gt;XYZ123456&lt;/Identifier&gt;
      &lt;/Class&gt;
    &lt;/Classes&gt;
  &lt;/Product&gt;
  &lt;Product id="XYZ002"&gt;
    &lt;Name&gt;That product name&lt;/Name&gt;
    &lt;Cur&gt;EUR&lt;/Cur&gt;
    &lt;Tag&gt;More Text&lt;/Tag&gt;
    &lt;Classes&gt;
      &lt;Class id="EUR"&gt;
        &lt;ClassCur&gt;EUR&lt;/ClassCur&gt;
        &lt;Identifier&gt;VDSHG123456&lt;/Identifier&gt;
      &lt;/Class&gt;
    &lt;/Classes&gt;
  &lt;/Product&gt;
&lt;/Products&gt;
</code></pre>

<p>The first thing I have been trying to accomplish but have so far failed to do is to extract all of the Product and Class id's  <code>"ABC001"</code>, <code>"XYZ002"</code> etc..</p>

<p>What I have tried is </p>

<pre><code>products = soup.find_all("Product")

for p in products:
    print(p.find("name")) # gets the name tag
    print(p.find("cur")) # gets the cur tag
    # ...etc
</code></pre>

<p>However, I can't figure out how to access <code>id</code> within <code>Product</code>. For example, <code>p.find("product")</code> returns <code>None</code>.</p>

<p>Note that while I am using bs4 I don't <em>have</em> to - it's just that I have done a lot of web scraping with Python + bs4 and have found bs4 to be useful in navigating through HTML, so assumed it would be the ideal way of handling XML.</p>

<p><code>id</code> is an attribute of <code>Product</code>, not a child element, so you access it with:</p>

<pre><code>p['id']
</code></pre>

python xml bs4