title: How to get an attribute value using BeautifulSoup and Python?
body: <p>I'm failing miserably to get an attribute value using BeautifulSoup and Python. Here is how the XML is structured:</p>
  
  <pre><code>...
  &lt;/total&gt;
  &lt;tag&gt;
      &lt;stat fail="0" pass="1"&gt;TR=111111 Sandbox=3000613&lt;/stat&gt;
      &lt;stat fail="0" pass="1"&gt;TR=121212 Sandbox=3000618&lt;/stat&gt;
      ...
      &lt;stat fail="0" pass="1"&gt;TR=999999 Sandbox=3000617&lt;/stat&gt;
  &lt;/tag&gt;
  &lt;suite&gt;
  ...
  </code></pre>
  
  <p>What I'm trying to get is the <code>pass</code> value, but for the life of me I just can't understand how to do it. I checked the <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attributes" rel="noreferrer">BeautifulSoup</a> and it seems that I should be using something like <code>stat['pass']</code>, but that doesn't seem to work.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>with open('../results/output.xml') as raw_resuls:
  results = soup(raw_resuls, 'lxml')
  for stat in results.find_all('tag'):
              print stat['pass']
  </code></pre>
  
  <p>If I do <code>results.stat['pass']</code> it returns a value that is within another tag, way up in the XML blob.</p>
  
  <p>If I print the <code>stat</code> variable I get the following:</p>
  
  <pre><code>&lt;stat fail="0" pass="1"&gt;TR=787878 Sandbox=3000614&lt;/stat&gt;
  ...
  &lt;stat fail="0" pass="1"&gt;TR=888888 Sandbox=3000610&lt;/stat&gt;
  </code></pre>
  
  <p>Which seems to be ok.</p>
  
  <p>I'm pretty sure that I'm missing something or doing something wrong. Where should I be looking at? Am I taking the wrong approach?</p>
  
  <p>Any advice or guidance will be greatly appreciated! Thanks</p>