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

book
Challenge: Iterating Over Lists

Tehtävä

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).

Ratkaisu

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 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(___)
toggle bottom row
some-alt