I'm using isinstance to select some html tags and passing them to a Beautifulsoup function. The problem is I keep getting NameErrors from what should be perfectly executable code.
def horse_search(tag):
return (tag.has_attr('href') and isinstance(tag.previous_element, span))
...
for tag in soup.find_all(horse_search):
print (tag)
NameError: global name 'span' is not defined
Also I'm getting errors from the example code in the documentation of Beautifulsoup using isinstance in conjunction with tag.previous_element
def surrounded_by_strings(tag):
return (isinstance(tag.next_element, NavigableString)
and isinstance(tag.previous_element, NavigableString))
for tag in soup.find_all(surrounded_by_strings):
print tag.name
NameError: global name "NavigableString" is not defined
What could be wrong? Thanks!