title: How to extract tags from HTML using Beautifulsoup in Python
body: <p>I am trying to parse through an HTML page which simplified looks like this:</p>
  
  <pre><code>&lt;div class="anotherclass part"
    &lt;a href="http://example.com" &gt;
      &lt;div class="column abc"&gt;&lt;strike&gt;&amp;#163;3.99&lt;/strike&gt;&lt;br&gt;&amp;#163;3.59&lt;/div&gt;
      &lt;div class="column def"&gt;&lt;/div&gt;
      &lt;div class="column ghi"&gt;1 Feb 2013&lt;/div&gt;
      &lt;div class="column jkl"&gt;
        &lt;h4&gt;A title&lt;/h4&gt;
        &lt;p&gt;
          &lt;img class="image" src="http://example.com/image.jpg"&gt;A, List, Of, Terms, To, Extract - 1 Feb 2013&lt;/p&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I am a beginner at coding python and I have read and re-read the beautifulsoup documentation at <a href="http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html" rel="nofollow">http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html</a></p>
  
  <p>I have got this code:</p>
  
  <pre><code>from BeautifulSoup import BeautifulSoup
  
  with open("file.html") as fp:
    html = fp.read()
  
  soup = BeautifulSoup(html)
  
  parts = soup.findAll('a', attrs={"class":re.compile('part'), re.IGNORECASE} )
  for part in parts:
    mypart={}
  
    # ghi
    mypart['ghi'] = part.find(attrs={"class": re.compile('ghi')} ).string
    # def
    mypart['def'] = part.find(attrs={"class": re.compile('def')} ).string
    # h4
    mypart['title'] = part.find('h4').string
  
    # jkl
    mypart['other'] = part.find('p').string
  
    # abc
    pattern = re.compile( r'\&amp;\#163\;(\d{1,}\.?\d{2}?)' )
    theprices = re.findall( pattern, str(part) )
    if len(theprices) == 2:
      mypart['price'] = theprices[1]
      mypart['rrp'] = theprices[0]
    elif len(theprices) == 1:
      mypart['price'] = theprices[0]
      mypart['rrp'] = theprices[0]
    else:
      mypart['price'] = None
      mypart['rrp'] = None
  </code></pre>
  
  <p>I want to extract any text from the classes <code>def</code> and <code>ghi</code> which I think my script does correctly.</p>
  
  <p>I also want to extract the two prices from <code>abc</code> which my script does in a rather clunky fashion at the moment.  Sometimes there are two prices, sometimes one and sometimes none in this part.</p>
  
  <p>Finally I want to extract the <code>"A, List, Of, Terms, To, Extract"</code> part from class <code>jkl</code> which my script fails to do.  I thought getting the string part of the <code>p</code> tag would work but I cannot understand why it does not.  The date in this part always matches the date in class <code>ghi</code> so it should be easy to replace/remove it.</p>
  
  <p>Any advice?  Thank-you!</p>