使用 BeautifulSoup 在 HTML 文档中查找具有给定属性值的标签
先决条件: Beautifulsoup
在本文中,我们将讨论如何使用 beautifulsoup 在 HTML 文档中查找具有给定属性值的标签。
方法:
- 导入模块。
- 从网页中抓取数据。
- 将抓取的字符串解析为 HTML。
- 使用 find()函数查找属性和标签。
- 打印结果。
Syntax: find(attr_name=”value”)
下面是上述方法的一些实现:
示例 1:
Python3
# importing module
from bs4 import BeautifulSoup
markup = '''Div Content'''
soup = BeautifulSoup(markup, 'html.parser')
# finding the tag with the id attribute
div_bs4 = soup.find(id = "container")
print(div_bs4.name)
Python3
# importing module
from bs4 import BeautifulSoup
markup = '''Geeks for Geeks'''
soup = BeautifulSoup(markup, 'html.parser')
# finding the tag with the href attribute
div_bs4 = soup.find(href = "https://www.geeksforgeeks.org/")
print(div_bs4.name)
Python3
# importing module
from bs4 import BeautifulSoup
markup = """Welcome to geeksforgeeks
Geeks
geeksforgeeks a computer science portal for geeks
"""
soup = BeautifulSoup(markup, 'html.parser')
# finding the tag with the class attribute
div_bs4 = soup.find(class_ = "gfg")
print(div_bs4.name)
输出:
div
示例 2:
蟒蛇3
# importing module
from bs4 import BeautifulSoup
markup = '''Geeks for Geeks'''
soup = BeautifulSoup(markup, 'html.parser')
# finding the tag with the href attribute
div_bs4 = soup.find(href = "https://www.geeksforgeeks.org/")
print(div_bs4.name)
输出:
a
示例 3:
蟒蛇3
# importing module
from bs4 import BeautifulSoup
markup = """Welcome to geeksforgeeks
Geeks
geeksforgeeks a computer science portal for geeks
"""
soup = BeautifulSoup(markup, 'html.parser')
# finding the tag with the class attribute
div_bs4 = soup.find(class_ = "gfg")
print(div_bs4.name)
输出:
p