BeautiflSoup -- getting all the attributes of a tag?

7stud bbxx789_05ss at yahoo.com
Tue Apr 1 19:25:28 EDT 2008


You can treat a tag like a dictionary to obtain a specific attribute:

import BeautifulSoup as bs

html = "<div x='a' y='b' z='c'>hello</div>"

doc = bs.BeautifulSoup(html)
div = doc.find("div")
print div
print div["x"]

--output:--
a

But you can't iterate over a tag to get all the attributes:

import BeautifulSoup as bs

html = "<div x='a' y='b' z='c'>hello</div>"

doc = bs.BeautifulSoup(html)
div = doc.find("div")

for key in div:
    print key, div[key]

--output:--
hello
Traceback (most recent call last):
  File "test1.py", line 9, in ?
    print key, div[key]
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/
python2.4/site-packages/BeautifulSoup.py", line 430, in __getitem__
    return self._getAttrMap()[key]
KeyError: u'hello'

How can you get all the attributes when you don't know the attribute
names ahead of time?



More information about the Python-list mailing list