title: Parsing html using BeautifulSoup in Python
body: <p>I wrote some code to parse html, but the result was not what I wanted:</p>
  
  <pre><code>import urllib2
  html = urllib2.urlopen('http://dummy').read()
  from BeautifulSoup import BeautifulSoup
  soup = BeautifulSoup(html)
  for definition in soup.findAll('span', {"class":'d'}):
  definition = definition.renderContents()
  print "&lt;meaning&gt;", definition
  for exampleofuse in soup.find('span',{"class":'x'}):
      print "&lt;exampleofuse&gt;", exampleofuse, "&lt;exampleofuse&gt;"
  print "&lt;meaning&gt;"
  </code></pre>
  
  <p>Is there any kind of way that when class attribute is "d" or "x" to then get the string?</p>
  
  <p>The following html code is what I want to parse:</p>
  
  <pre><code>&lt;span class="d"&gt;calculated by adding several amounts together&lt;/span&gt;
  &lt;span class="x"&gt;an average rate&lt;/span&gt;
  &lt;span class="x"&gt;at an average speed of 100 km/h&lt;/span&gt;
  &lt;span class="d"&gt;typical or normal&lt;/span&gt;
  &lt;span class="x"&gt;average intelligence&lt;/span&gt;
  &lt;span class="x"&gt;20 pounds for dinner is average&lt;/span&gt;
  </code></pre>
  
  <p>Then, this is the result I want:</p>
  
  <pre><code>&lt;definition&gt;calculated by adding several amounts together
      &lt;example_of_use&gt;an average rate&lt;/example_of_use&gt;
      &lt;example_of_use&gt;at an average speed of 100 km/h&lt;/example_of_use&gt;
  &lt;/definition&gt;
  &lt;definition&gt;typical or normal
      &lt;example_of_use&gt;average intelligence&lt;/example_of_use&gt;
      &lt;example_of_use&gt;20 pounds for dinner is average&lt;/example_of_use&gt;
  &lt;/definition&gt;
  </code></pre>