Check element type in BeautifulSoup 3

后端 未结 1 1291
一向
一向 2021-01-04 13:10

How can I check if a Tag element is of a certain type, for example a div, in BS3?

相关标签:
1条回答
  • 2021-01-04 13:24

    You are looking for the tag name:

    if element.name == 'div':
    

    Demo:

    >>> from bs4 import BeautifulSoup
    >>> soup = BeautifulSoup('<div><span></span></div>')
    >>> print soup.find('div').name
    div
    

    This attribute hasn't changed between BeautifulSoup 3 and 4. I strongly recommend you use BeautifulSoup 4; all development on BS3 has stopped, the last release for that version was over 2 years ago.

    0 讨论(0)
提交回复
热议问题