Artyom
Artyom

Reputation: 3063

How can I get all the attributes of a HTML tag?

How can I get all the attributes of a HTML tag?

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

Upvotes: 0

Views: 216

Answers (2)

Yuda Prawira
Yuda Prawira

Reputation: 12461

use pretiffy() in BeautifulSoup

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

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 837986

Use attrs:

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

Upvotes: 2

Related Questions