在Python使用 BeautifulSoup 从给定的 html 文档中查找标题标签
让我们看看如何在Python使用 BeautifulSoup 从给定的 html 文档中查找标题标签。所以我们可以使用 BeautifulSoup find() 方法从 html 文档中找到标题标签。 find函数将标签的名称作为字符串输入,并从网页中返回第一个找到的特定标签的匹配项。
示例 1:
Python
# import BeautifulSoup
from bs4 import BeautifulSoup
# create html document
html = """
GREEKSFORGREEKS
GFG BeautifulSoup tutorial
"""
# invoke BeautifulSoup()
soup = BeautifulSoup(html, 'html.parser')
print(" *** Title of the document *** ")
# invoke find()
print(soup.find("title"))
Python
# import BeautifulSoup
from bs4 import BeautifulSoup
# create html document
html = """
Hi I am Title
GFG BeautifulSoup tutorial
"""
# invoke BeautifulSoup()
soup = BeautifulSoup(html, 'html.parser')
print(" *** Title of the document *** ")
# invoke find()
print(soup.find("title"))
输出:
示例 2:
Python
# import BeautifulSoup
from bs4 import BeautifulSoup
# create html document
html = """
Hi I am Title
GFG BeautifulSoup tutorial
"""
# invoke BeautifulSoup()
soup = BeautifulSoup(html, 'html.parser')
print(" *** Title of the document *** ")
# invoke find()
print(soup.find("title"))
输出: