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:
- Assign all the occurrences of
<p>
elements to thep
variable. - Output number of elements within the
p
variable (p
is a list).
Solución
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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?
¡Gracias por tus comentarios!
Sección 2. Capítulo 6
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla