Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Attributes and Contents of Multiple Elements | Working with Element Attributes in Beautiful Soup
Web Scraping with Python

bookAttributes 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)
copy

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())
copy
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

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

bookAttributes 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)
copy

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())
copy
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
some-alt