让 BeautifulSoup 以不区分大小写的方式捕获标签

发布于 2024-09-11 23:19:48 字数 303 浏览 2 评论 0原文

我想用 BeautifulSoup 捕获一些标签:一些

标签、</code> 标签、一些 <code><meta></code> 标签。但无论他们的情况如何,我都想抓住他们;我知道有些网站会像这样进行元操作: <code><META></code> 我希望能够捕捉到这一点。

我注意到 BeautifulSoup 默认区分大小写。如何以不区分大小写的方式捕获这些标签?

I want to catch some tags with BeautifulSoup: Some <p> tags, the <title> tag, some <meta> tags. But I want to catch them regardless of their case; I know that some sites do meta like this: <META> and I want to be able to catch that.

I noticed that BeautifulSoup is case-sensitive by default. How do I catch these tags in a non-case-sensitive way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尴尬癌患者 2024-09-18 23:19:48

BeautifulSoup 标准化了输入的解析树。它将标签转换为小写。 IMO 你不用担心。

BeautifulSoup standardises the parse tree on input. It converts tags to lower-case. You don't have anything to worry about IMO.

没企图 2024-09-18 23:19:48

您可以使用 soup.findAll 应该不区分大小写匹配:

import BeautifulSoup

html = '''<html>
<head>
<meta name="description" content="Free Web tutorials on HTML, CSS, XML" /> 
<META name="keywords" content="HTML, CSS, XML" /> 
<title>Test</title>
</head>
<body>
</body>
</html>'''

soup = BeautifulSoup.BeautifulSoup(html)
for x in soup.findAll('meta'):
    print x

结果:

<meta name="description" content="Free Web tutorials on HTML, CSS, XML" />
<meta name="keywords" content="HTML, CSS, XML" />

You can use soup.findAll which should match case-insensitively:

import BeautifulSoup

html = '''<html>
<head>
<meta name="description" content="Free Web tutorials on HTML, CSS, XML" /> 
<META name="keywords" content="HTML, CSS, XML" /> 
<title>Test</title>
</head>
<body>
</body>
</html>'''

soup = BeautifulSoup.BeautifulSoup(html)
for x in soup.findAll('meta'):
    print x

Result:

<meta name="description" content="Free Web tutorials on HTML, CSS, XML" />
<meta name="keywords" content="HTML, CSS, XML" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文