如何检查迭代变量NavigableString或Tag类型?

时间:2017-01-20 08:00:40

标签: python python-3.x beautifulsoup

我将列表与Tag和NavigableString Type混合在一起。我想只访问Tag数据。我怎么检查?我想避免以下错误。

AttributeError: type object 'BeautifulSoup' has no attribute 'NavigableString'

1 个答案:

答案 0 :(得分:3)

from bs4 import Tag, NavigableString, BeautifulSoup
html = r"<b>The Dormouse's story</b>"

soup = BeautifulSoup(html, 'lxml')

print(isinstance(soup, Tag))
print(isinstance(soup.string, NavigableString))

出:

True
True