问题
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!
回答1:
to find all anchors that has a span parent and an href attribute do:
for span in soup.find_all('span'):
for a in span.find_all('a'):
if a.has_attr('href'):
print a['href']
however, while this is nice, as in most cases, using some tool that supports xpath can be even better, for example, using lxml and xpath you code can look as neat as:
from lxml import etree
etree.parse(url, etree.HTMLParser()).xpath('//span/a/@href')
来源:https://stackoverflow.com/questions/21819763/isinstance-not-working-correctly-with-beautifulsoupnameerror