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

Opgave

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)

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4
single

single

# 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ørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt