Challenge: Count Number of Images
Opgave
Swipe to start coding
You have successfully opened, read, and decoded the page. Your tasks are:
- Count the number of
div
tags within theweb_page
. - Count the number of images (
img
) within theweb_page
.
To identify these elements, search for the strings "<div"
and "<img"
, as elements may include attributes.
Løsning
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Importing the module
from urllib.request import urlopen
# Opening web page
url = "https://codefinity-content-media.s3.eu-west-1.amazonaws.com/18a4e428-1a0f-44c2-a8ad-244cd9c7985e/page.html"
page = urlopen(url)
# Reading and decoding
web_page = page.read().decode('utf-8')
# Count number of divs
print(web_page.count("<div"))
# Count number of images
print(web_page.count("<img"))
Var alt klart?
Tak for dine kommentarer!
Sektion 1. Kapitel 11
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Importing the module
from urllib.request import urlopen
# Opening web page
url = "https://codefinity-content-media.s3.eu-west-1.amazonaws.com/18a4e428-1a0f-44c2-a8ad-244cd9c7985e/page.html"
page = urlopen(url)
# Reading and decoding
web_page = page.read().decode("utf-8")
# Count number of divs
print(web_page.count("___"))
# Count number of images
print(___.___("___"))
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat