Python BeautifulSoup:使用 name 获取 HTML 标签名


#Python BeautifulSoup 教程


简介

可以通过 name 获取标签名。

示例

代码:

from bs4 import BeautifulSoup

html_content = '''
<div>测试01</div>
<div>测试02</div>
<h1>测试H1</h1>
'''
soup = BeautifulSoup(html_content, 'html.parser')

for ele in soup.find_all(['h1', 'h2', 'div']):
    print(ele.name)

执行结果:

div
div
h1


( 本文完 )