Challenge: Count Number of Images
Task
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.
Solution
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"))
Everything was clear?
Thanks for your feedback!
Section 1. Chapter 11
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(___.___("___"))
Ask AI
Ask anything or try one of the suggested questions to begin our chat