Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether or not a iterating variable NavigableString or Tag type?

I got the list mixed with Tag and NavigableString Type. I want to access only Tag data. How can I check? I want to avoid the following error.

AttributeError: type object 'BeautifulSoup' has no attribute 'NavigableString'
like image 338
verystrongjoe Avatar asked Jan 20 '17 08:01

verystrongjoe


1 Answers

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))

out:

True
True
like image 69
宏杰李 Avatar answered Nov 12 '22 12:11

宏杰李