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())
Everything was clear?
Thanks for your feedback!
Sectionย 3. Chapterย 3
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Sectionย 3. Chapterย 3