Beautiful soup tag attributes - Dictionary?

Chris Rebert clp at rebertia.com
Mon Dec 1 00:04:44 EST 2008


On Sun, Nov 30, 2008 at 8:51 PM, killsto <kiliansto at gmail.com> wrote:
> The documentation says I can find attributes of tags by using it as a
> dictionary. Ex:
>
> product = p.findAll('dd')

.findAll() produces a *list* of tags

The example in the docs is:
firstPTag, secondPTag = soup.findAll('p')

which is using *tuple unpacking*, so it's equivalent to:

tags = soup.findAll('p')
firstPTag = tags[0] #slicing the list to get individual item
secondPTag = tags[1]

So, you either need to use .find() instead of .findAll(), or treat the
result of .findAll() correctly as a list of tags.

Cheers,
Chris
-- 
Follow the path of the Iguana...
http://rebertia.com

> print product['id']
>
> However, when I try that python thinks I am slicing it. When I print
> product, it works but is a list. I am pretty sure I have the latest
> version.
>
> Any ideas?
>
> Reference: http://www.crummy.com/software/BeautifulSoup/documentation.html#The%20attributes%20of%20Tags
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list