Challenge: Iterating Over Lists
Oppgave
Swipe to start coding
You will work with the same page. In this task, you need to iterate over unordered list (the <ul>
element) and print all its elements. Follow the next steps:
- Iterate over all children of the
ul
element using theel
as iterator. - Within each loop step, display the iterated element (
el
).
Løsning
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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")
# Iterate over all children on the `ul` element
for el in soup.ul.children:
# Display iterated element
print(el)
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 2. Kapittel 4
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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")
# Iterate over all children on the `ul` element
for el in soup.___.___:
# Display iterated element
print(___)
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår