Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Iterating Over Lists | Decoding HTML with Beautiful Soup
Web Scraping with Python

book
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:

  1. Iterate over all children of the ul element using the el as iterator.
  2. Within each loop step, display the iterated element (el).

Løsning

# 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?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 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/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

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt