Ram*_*hum 5 html python parsing beautifulsoup case-insensitive
我想用 BeautifulSoup 捕获一些标签:一些<p>标签,<title>标签,一些<meta>标签。但无论他们的情况如何,我都想抓住他们;我知道有些网站会这样做元:<META>我希望能够抓住这一点。
我注意到 BeautifulSoup 默认区分大小写。如何以不区分大小写的方式捕获这些标签?
Mar*_*ers -1
您可以使用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
Run Code Online (Sandbox Code Playgroud)
结果:
<meta name="description" content="有关 HTML、CSS、XML 的免费 Web 教程" /> <meta name="keywords" content="HTML、CSS、XML" />