Beautiful soup tag attributes - Dictionary?

C

Chris Rebert

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 attributes of Tags
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top