Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Text from HTML Elements | Working with Element Attributes in Beautiful Soup
Web Scraping with Python

book
Challenge: Text from HTML Elements

Task

Swipe to start coding

You are given a web page. Complete the following tasks:

  1. Print all the attributes of all the <a> elements.

  2. Print all the text of all the <p> elements.

Solution

# Import libraries
from bs4 import BeautifulSoup
from urllib.request import urlopen

# Open the page
url = "https://codefinity-content-media.s3.eu-west-1.amazonaws.com/18a4e428-1a0f-44c2-a8ad-244cd9c7985e/page.html"
page = urlopen(url)
html = page.read().decode("utf-8")

# Create the BeautifulSoup object
soup = BeautifulSoup(html, "html.parser")

# Print attributes of all the a elements
for a in soup.find_all("a"):
print(a.attrs)

# Print only the text of all the p tags
for p in soup.find_all("p"):
print(p.get_text())
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 4
# Import libraries
from bs4 import BeautifulSoup
from urllib.request import urlopen

# Open the page
url = "https://codefinity-content-media.s3.eu-west-1.amazonaws.com/18a4e428-1a0f-44c2-a8ad-244cd9c7985e/page.html"
page = urlopen(url)
html = page.read().decode("utf-8")

# Create the BeautifulSoup object
soup = BeautifulSoup(html, "html.parser")

# Print attributes of all the a elements
for i in ____.___("___"):
print(i.___)

# Print only the text of all the p tags
for i in soup.___("___"):
print(i.___)
toggle bottom row
some-alt