使用 Python 中的 Beautifulsoup 獲取標籤名稱


BeautifulSoup 被認為是最廣泛使用的 Python 網路爬蟲包之一。它是用於解析 HTML 和 XML 文件最棒的工具之一,它使從網站提取資料變得更簡單快捷。對於特定 HTML 和 XML 元素提取標籤名稱是網路爬蟲中最常見的任務之一。在處理 HTML 和 XML 文件時,獲取給定元素的標籤名稱是最常見的任務之一。

可以使用以下命令安裝 Python 的 BeautifulSoup 庫

pip install beautifulsoup4

方法

  • 使用 name 屬性

方法 1:使用 name 屬性

此方法包括使用 BeautifulSoup 獲取標籤名稱,即 Tag 物件的 name 屬性。此屬性返回標籤名稱的字串值。以下是 name 屬性的語法

語法

tag.name

返回型別 包含 Tag 名稱的字串值。

演算法

  • 匯入 BeautifulSoup 模組。

  • 定義一個將用於從中獲取標籤的 HTML 多行字串。

  • 透過向 BeautifulSoup 建構函式提供 HTML 文件和解析器作為輸入來建立 BeautifulSoup 物件。在本例中,使用 html.parser 作為解析器。

  • 使用 soup.find() 方法查詢文件中 `

    ` 標籤的第一次出現。

  • 使用 name 屬性獲取 p Tag 物件的名稱。

  • 使用 print() 語句列印標籤名稱。

示例 1

以下是演示此方法的示例程式碼

from bs4 import BeautifulSoup

# HTML document to be parsed
html_doc = """
<html>
<head>
   <title>TutorialsPoint</title>
</head>
<body>
   <p>TutorialsPoint</p>
</body>
</html>
"""

# Parse the HTML document using BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')

# Get the first <p> tag in the HTML document
p_tag = soup.find('p')

# Get the tag name using the name attribute
tag_name = p_tag.name

# Print the tag name
print("Tag name is:", tag_name)

輸出

Tag name is: p

示例 2

在此示例中,我們正在解析 XML 文件並從自定義標籤獲取標籤名稱。

from bs4 import BeautifulSoup

xml_doc = '''
<book>
    <title>Harry Potter</title>
    <author>J.K. Rowling</author>
    <publisher>Bloomsbury</publisher>
</book>
'''

# Parse the XML document using BeautifulSoup
soup = BeautifulSoup(xml_doc, 'xml')

# Get the first <author> tag in the XML document
tag = soup.find('author')

# Get the tag name using the name attribute
tag_name = tag.name

# Print the tag name
print("Tag name is:", tag_name)

輸出

Tag name is: author

示例 3

在此示例中,我們使用其類獲取標籤,然後應用 name 屬性來獲取標籤的名稱。

from bs4 import BeautifulSoup

# HTML document to be parsed
html_doc = """
<html>
<head>
   <title class="tut">TutorialsPoint</title>
</head>
<body>
   <p>TutorialsPoint</p>
</body>
</html>
"""

# Parse the HTML document using BeautifulSoup constructor
soup = BeautifulSoup(html_doc, 'html.parser')

# Get the tag using its class
p_tag = soup.find(class_='tut')

# Get the tag name using the name attribute
tag_name = p_tag.name

# Print the tag name
print("Tag name is:", tag_name)

輸出

Tag name is: title

示例 4

在此示例中,我們使用其 id 獲取標籤,然後應用 name 屬性來獲取標籤的名稱。

from bs4 import BeautifulSoup

# HTML document to be parsed
html_doc = """
<html>
<head>
   <title id="tut">TutorialsPoint</title>
</head>
<body>
   <p>TutorialsPoint</p>
</body>
</html>
"""

# Parse the HTML document using BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')

# Get the tag using its id
p_tag = soup.find(id='tut')

# Get the tag name using the name attribute
tag_name = p_tag.name

# Print the tag name
print("Tag name is:", tag_name)

輸出

Tag name is: title

結論

可以說 BeautifulSoup 是一個強大的 Python 模組,它使解析 HTML 和 XML 文字變得簡單。它提供各種工具和選項來搜尋、導航和修改文件樹。

每個示例根據使用的方法或函式都有其自身的優缺點。您可以根據想要使用的表示式的複雜性和編寫程式碼的個人喜好來選擇所需的方法。

更新於:2023年5月29日

3K+ 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.