如何获取HTML标记的所有属性?

时间:2010-08-22 20:20:03

标签: python beautifulsoup

如何获取HTML标记的所有属性?

listinp = soup('input')
for input in listinp:
    # get all attr on this tag in dict

2 个答案:

答案 0 :(得分:2)

使用attrs

for tag in listinp:
    print dict(tag.attrs)

答案 1 :(得分:0)

在BeautifulSoup中使用prettify()

import urllib2, BeautifulSoup
opener = urllib2.build_opener()
host = "http://google.com"
site = opener.open(host)
html = site.read()
soup = BeautifulSoup(html)
print soup.pretiffy()