Categories
Python Answers

How to extract an attribute value with Python beautifulsoup?

Spread the love

Sometimes, we want to extract an attribute value with Python beautifulsoup.

In this article, we’ll look at how to extract an attribute value with Python beautifulsoup.

How to extract an attribute value with Python beautifulsoup?

To extract an attribute value with Python beautifulsoup, we can use the list of dicts returned by findAll.

For instance, we write

import urllib

f = urllib.urlopen("http://example.com")
s = f.read()
f.close()

from BeautifulSoup import BeautifulStoneSoup

soup = BeautifulStoneSoup(s)

input_tags = soup.findAll(attrs={"name": "stainfo"})

output = [x["stainfo"] for x in input_tags ]

print(output)

to parse the HTML returned from urlopen with

soup = BeautifulStoneSoup(s)

Then we get all the elements with the attribute name stainfo with

soup.findAll(attrs={"name": "stainfo"})

And then we get the stainfo attribute value of each element with

[x["stainfo"] for x in input_tags ]

Conclusion

To extract an attribute value with Python beautifulsoup, we can use the list of dicts returned by findAll.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *