如何使用Beautiful Soup查找带有特定文本的标签?

THE_:

我有以下html(用标记的换行符\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>
...

如何查找要查找的文本下面的代码返回第一个找到的值,因此我需要以固定文本进行过滤

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

更新如果我使用以下代码:

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

然后它只返回固定文本:

user130076:

您可以将正则表达式传递给的text参数findAll,如下所示:

import BeautifulSoup
import re

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Beautiful Soup查找带有特定文本的标签?

查找带有特定文本的<td>标签值(Beautiful Soup)

Python 3 Beautiful Soup查找带有冒号的标签

当有多个相似标签时,使用 Beautiful Soup 从特定 HTML 标签中提取文本

如何使用 Beautiful Soup 在 `p` 标签中获取文本?

如何使用scrapy或beautiful Soup提取特定html标签的内容?

如何在Beautiful Soup中查找特定的div文本

无法使用Python的Beautiful Soup从特定的span标签提取文本

如何使用Beautiful Soup来<script>标签?

如何使用Beautiful Soup查找节点

Python-如何使用Beautiful Soup查找ID为“ value”的所有跨度的文本?

如何使用Beautiful Soup从HTML获取文本

如何使用Beautiful Soup查找具有自定义html属性的所有元素,而不管html标签如何?

Beautiful Soup - 如何在 HTML 中的特定项目后查找标签?

如何使用Beautiful Soup按属性值选择标签

如何在Beautiful Soup中的标签上使用if语句?

使用Python和Beautiful Soup如何捕获空标签

如何使用 Beautiful Soup 4 删除空的 <p> 标签

如何使用Beautiful Soup在Python中的span标签内抓取文本

如何使用Beautiful Soup在HTML中查找文本的下一个实例?

如何使用Beautiful Soup修改xml?

如何使用Beautiful Soup删除html注释

如何使用Beautiful Soup从HTML提取特定的脚本元素

如何在终端中查找带有特定文本的文件

如何通过Python的Beautiful Soup在班级和班级名称中找到带有空格的文本?

Python Beautiful Soup:如何提取标签旁边的文本?

在Beautiful Soup中定位没有标签的文本元素

使用 Beautiful Soup 提取文本

如何在Beautiful Soup中提取具有相同标签的特定嵌套元素?