BeautifulSoup fetch help
Mike Meyer
mwm at mired.org
Sat Jan 7 00:15:38 EST 2006
"ted" <tedPLEASE-NO-SPAM-95050 at sbcglobal.net> writes:
> I'm using the BeautifulSoup module and having some trouble processing a
> file. It's not printing what I'm expecting. In the code below, I'm expecting
> cells with only "bgcolor" attributes to be printed, but I'm getting cells
> with other attributes and some without any attributes.
BeatifulSoups matching is for any tag with a matching attribute, not
tags that only match that attribute. That's why you're getting tags
with other attributes.
However, you can use a callable as the tag argument to check for what
you want:
def findtagswithly(name, attr):
return (lambda tag: tag.name == name and
len(tag.attrs) == 1 and
tag.attrs[0][0] == attr)
...
cells = table.fetch(findtagswithonly('a', 'bgcolor'))
Or, because I wrote it to check out:
def findtagswithoneattrib(name):
return lambda tag: tag.name == name and len(tag.attrs) == 1
...
cells = table.fetch(findtagswithoneattrib('a', {bgcolor: re.compile('.+)}))
I'm not sure why you're getting tags without attributes. If the above
code does that, post some sample data along with the code.
<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list
mailing list