获取BeautifulSoup以不区分大小写的方式捕获标签 [英] Getting BeautifulSoup to catch tags in a non-case-sensitive way

查看:68
本文介绍了获取BeautifulSoup以不区分大小写的方式捕获标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用BeautifulSoup捕获一些标签:一些< p> 标签,< title> 标签,一些< meta> 标签.但是我想抓住他们,不管他们是什么情况.我知道有些网站的元数据是这样的:< META> ,我希望能够抓住它.

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.

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

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

推荐答案

您可以使用

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

结果:


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

这篇关于获取BeautifulSoup以不区分大小写的方式捕获标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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