溫馨提示×

BeautifulSoup中怎么獲取Tag對象的名字

小億
96
2024-05-14 13:36:59
欄目: 編程語言

在BeautifulSoup中,要獲取Tag對象的名字,你可以使用`.name`屬性。以下是一個例子:

```python

from bs4 import BeautifulSoup

html = '

這是一個段落。

'

soup = BeautifulSoup(html, 'html.parser')

tag = soup.p # 獲取

標簽

tag_name = tag.name # 獲取標簽名字

print(tag_name) # 輸出: 'p'

```

在這個例子中,我們首先導(dǎo)入了`BeautifulSoup`庫,然后解析了一個簡單的HTML字符串。接著,我們通過`soup.p`獲取了`

`標簽對象,并使用`.name`屬性獲取了它的名字(即`'p'`)。

0