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 find_all
method.
For instance, we write:
import requests
from bs4 import BeautifulSoup
r = requests.get("https://www.crummy.com/software/BeautifulSoup/bs4/doc/")
soup = BeautifulSoup(r.text, 'html.parser')
res = soup.find_all(attrs={"class": 'document'})
print(res)
We make a GET request to get the content of https://www.crummy.com/software/BeautifulSoup/bs4/doc/.
Then we get the HTML text with r.text
and use that as the argument of the BeautifulSoup
constructor.
Then we find all the elements with the class
attribute set to document
with:
a = soup.find_all(attrs={"class": 'document'})
Conclusion
To extract an attribute value with Python BeautifulSoup, we can use the find_all
method.