【问题标题】:How to exclude the title tag from bs4 string searches with negated regular expressions如何使用否定正则表达式从 bs4 字符串搜索中排除标题标签
【发布时间】:2017-12-01 09:02:10
【问题描述】:

我想使用 bs4 搜索带有特定字符串的第一个标签,并从搜索中排除标题标签。根据bs4 doc,它支持正则表达式。

为什么下面的代码不起作用:

#!/usr/bin/env python
from bs4 import BeautifulSoup
import re

html_doc = """
<html>
<head>
    <title>The Dormouse's story</title>
</head>
<body>
    <p class="title"><b>The Dormouse's story</b></p>
    <div class="title">The Dormouse's story</div>
</body>
</html>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
title = soup.find(name=re.compile("^title"), string="The Dormouse's story")

if title is not None:
    print("String found:", title)
else:
    print("String not found.")

# returns: String found: <title>The Dormouse's story</title>

(我希望代码能找到&lt;b&gt;The Dormouse's story&lt;/b&gt; 标签。)

bs4/Python 是否需要特殊的正则表达式语法?

注意:想要替代代码建议。我想知道我的代码是否有问题,或者 bs4 是否不支持正则表达式,这与文档所说的相反。

【问题讨论】:

    标签: python regex string python-3.x beautifulsoup


    【解决方案1】:

    如果你想过滤掉'titles'并匹配字符串试试这个

    def filter_out_titles(tag):
      return tag is not None and not tag.name == 'title'
    
    soup.find_all(filter_out_titles, string="The Dormouse's story")
    

    【讨论】:

    • 感谢您的代码!我很确定它会起作用,但我想知道为什么我的代码没有,即使 bs4 文档提到它支持 find() 的正则表达式。
    • 您需要添加括号,否则它将匹配以“title”开头的字符串soup.find_all(re.compile("[^title]"), string="The Dormouse's story")
    【解决方案2】:
    title = soup.find(class_=re.compile(r"title"), string="The Dormouse's story")
    

    class_ 参数可以让你按类名选择,你搜索的是标签名。

    【讨论】:

    • 感谢您的回答,但我正在寻找的实际标签没有唯一的类属性,或者可能根本没有任何类属性。 (我刚刚选择了默认的 bs4 示例 html 代码。)
    • 你试过类似soup.findAll(text = re.compile('your regex here'))的东西吗?
    • have you tried something like soup.findAll(text = re.compile('your regex here'))? 不,因为我只需要第一场比赛。
    • soup.findAll(text = re.compile('你的正则表达式'))[0]
    【解决方案3】:

    请检查打印语句的顺序,以确认脚本正在从 HTML 代码中寻找您确切想要解析的内容,该代码基本上是针对此标签的:

    &lt;b&gt;The Dormouse's story&lt;/b&gt;.

    代码:

    #!/usr/bin/env python
    from bs4 import BeautifulSoup
    import re
    
    html_doc = """
    <html>
    <head>
        <title>The Dormouse's story</title>
    </head>
    <body>
        <p class="title"><b>The Dormouse's story</b></p>
        <div class="title">The Dormouse's story</div>
    </body>
    </html>
    """
    
    soup = BeautifulSoup(html_doc, 'html.parser')
    title = soup.find(class_=re.compile("^title"), string="The Dormouse's story")
    print(title)
    
    string = soup.find(str(title), 'html.parser')
    final_string = soup.find('b')
    print(final_string)
    
    if final_string is not None:
        print("String found:", final_string)
    else:
        print("String not found.")
    

    输出:

    <p class="title"><b>The Dormouse's story</b></p>
    <b>The Dormouse's story</b>
    String found: <b>The Dormouse's story</b>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 2015-01-05
      • 2011-07-28
      • 1970-01-01
      相关资源
      最近更新 更多