BeautiflSoup -- getting all the attributes of a tag?
7stud
bbxx789_05ss at yahoo.com
Tue Apr 1 19:28:33 EDT 2008
On Apr 1, 5:25 pm, 7stud <bbxx789_0... at yahoo.com> wrote:
> 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?
I figured it out:
import BeautifulSoup as bs
html = "<div x='a' y='b' z='c'>hello</div>"
doc = bs.BeautifulSoup(html)
div = doc.find("div")
for attr, val in div.attrs:
print "%s:%s" % (attr, val)
--output:--
x:a
y:b
z:c
More information about the Python-list
mailing list