isinstance not working correctly with beautifulsoup(NameError)

杀马特。学长 韩版系。学妹 提交于 2019-12-25 16:58:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!