如何找到与美丽的汤特定的文本标签? [英] How to find tag with particular text with Beautiful Soup?

查看:130
本文介绍了如何找到与美丽的汤特定的文本标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的HTML(标记为\\ n换行符):

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>
...

如何找到文字我找?下面返回code首先发现的价值,所以我需要通过筛选的固定文本弄好了。

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 。如果我用下面的code:

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'))

则返回刚才的固定文本:

推荐答案

您可以通过一个常规的前pression到的findAll 的文本参数,如下所示:

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'})

这篇关于如何找到与美丽的汤特定的文本标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆