Skip to content Skip to sidebar Skip to footer

Isinstance Not Working Correctly With Beautifulsoup(nameerror)

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.

Solution 1:

to find all anchors that has a span parent and an href attribute do:

forspaninsoup.find_all('span'):
    forainspan.find_all('a'):
        ifa.has_attr('href'):
            printa['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')

Post a Comment for "Isinstance Not Working Correctly With Beautifulsoup(nameerror)"