I’m parsing some data from HTML by walking through elements at a certain level using nextSibling, and doing different things depending on the tag name and class of each element encountered.
e.g.,
if n.name == "p" and n.class == "poem": blah()
But this raises an error if the element doesn’t have a class or if it isn’t an instance of Tag and hence has no name.
Testing before accessing like this
if "name" in n:
always return false. I could check the type of the object returned by nextSibling to try to weed out NavigableString and Comment, but there’s got to be an easier way.
EDIT
Emailed the dev of BeautifulSoup with this question and he recommended testing with
n.get("class")
which returns None if “class” is unset, which makes it possible to just do:
if n.get("class") == "poem": blah()
Besides using
get()methodAnother option is to use
has_attr()(usehas_key()pre BeautifulSoup 4):