Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Working with Paragraph Elements | Decoding HTML with Beautiful Soup
Web Scraping with Python

book
Working with Paragraph Elements

Tarea

Swipe to start coding

You will work with the same page. In this task, you need to save all the <p> elements in a variable, and then display all the occurrences and number of the element in a html structure. Follow the next steps:

  1. Assign all the occurrences of <p> elements to the p variable.
  2. Output number of elements within the p variable (p is a list).

Solución

# 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/jesus.html"
page = urlopen(url)
html = page.read().decode("utf-8")

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

# Save the list with all the `<p>` elements
p = soup.find_all('p')
print(p)

# Output number of `<p>` elements
print(len(p))

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 6
# 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/jesus.html"
page = urlopen(url)
html = page.read().decode("utf-8")

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

# Save the list with all the `<p>` elements
p = ___.___('___')
print(p)

# Output number of `<p>` elements
print(___(___))

Pregunte a AI

expand
ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

some-alt