Attributes and Contents of Multiple Elements
All the methods covered in the previous chapter can be applied to elements with a specific tag, that is, to the result of the .find_all() method. However, since .find_all() returns a list, you must access attributes and methods for each element separately. Use a for loop for this purpose. For example, retrieve all attributes of all <div> elements.
12345678910111213# Importing libraries from bs4 import BeautifulSoup from urllib.request import urlopen # Reading web page url = "https://codefinity-content-media.s3.eu-west-1.amazonaws.com/18a4e428-1a0f-44c2-a8ad-244cd9c7985e/jesus.html" page = urlopen(url) html = page.read().decode("utf-8") # Reading HTML with BeautifulSoup soup = BeautifulSoup(html, "html.parser") for div in soup.find_all("div"): print(div.attrs)
The same approach works for extracting text. For example, get all the text from every <p> element.
12345678910111213# Importing libraries from bs4 import BeautifulSoup from urllib.request import urlopen # Reading web page url = "https://codefinity-content-media.s3.eu-west-1.amazonaws.com/18a4e428-1a0f-44c2-a8ad-244cd9c7985e/jesus.html" page = urlopen(url) html = page.read().decode("utf-8") # Reading HTML with BeautifulSoup soup = BeautifulSoup(html, "html.parser") for p in soup.find_all("p"): print(p.get_text())
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how the `.attrs` attribute works in BeautifulSoup?
What other tags can I use with `.find_all()` besides `<div>` and `<p>`?
How can I filter elements by class or id using BeautifulSoup?
Awesome!
Completion rate improved to 4.35
Attributes and Contents of Multiple Elements
Swipe to show menu
All the methods covered in the previous chapter can be applied to elements with a specific tag, that is, to the result of the .find_all() method. However, since .find_all() returns a list, you must access attributes and methods for each element separately. Use a for loop for this purpose. For example, retrieve all attributes of all <div> elements.
12345678910111213# Importing libraries from bs4 import BeautifulSoup from urllib.request import urlopen # Reading web page url = "https://codefinity-content-media.s3.eu-west-1.amazonaws.com/18a4e428-1a0f-44c2-a8ad-244cd9c7985e/jesus.html" page = urlopen(url) html = page.read().decode("utf-8") # Reading HTML with BeautifulSoup soup = BeautifulSoup(html, "html.parser") for div in soup.find_all("div"): print(div.attrs)
The same approach works for extracting text. For example, get all the text from every <p> element.
12345678910111213# Importing libraries from bs4 import BeautifulSoup from urllib.request import urlopen # Reading web page url = "https://codefinity-content-media.s3.eu-west-1.amazonaws.com/18a4e428-1a0f-44c2-a8ad-244cd9c7985e/jesus.html" page = urlopen(url) html = page.read().decode("utf-8") # Reading HTML with BeautifulSoup soup = BeautifulSoup(html, "html.parser") for p in soup.find_all("p"): print(p.get_text())
Thanks for your feedback!