How to find tag with particular text with Beautiful Soup?

LA_ picture LA_ · Jan 25, 2012 · Viewed 104.1k times · Source

I have the following html (line breaks marked with \n):

...
<tr>
  <td class="pos">\n
      "Some text:"\n
      <br>\n
      <strong>some value</strong>\n
  </td>
</tr>
<tr>
  <td class="pos">\n
      "Fixed text:"\n
      <br>\n
      <strong>text I am looking for</strong>\n
  </td>
</tr>
<tr>
  <td class="pos">\n
      "Some other text:"\n
      <br>\n
      <strong>some other value</strong>\n
  </td>
</tr>
...

How to find text I am looking for? The code below returns first found value, so I need to filter by Fixed text somehow.

result = soup.find('td', {'class' :'pos'}).find('strong').text

Upd. If I use the following code:

title = soup.find('td', text = re.compile(ur'Fixed text:(.*)', re.DOTALL), attrs = {'class': 'pos'})
self.response.out.write(str(title.string).decode('utf8'))

then it returns just Fixed text:.

Answer

user130076 picture user130076 · Jan 25, 2012

You can pass a regular expression to the text parameter of findAll, like so:

import BeautifulSoup
import re

columns = soup.findAll('td', text = re.compile('your regex here'), attrs = {'class' : 'pos'})