Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Count Number of Images | Getting Acquainted with HTML
Web Scraping with Python

book
Challenge: Count Number of Images

Task

Swipe to start coding

You have successfully opened, read, and decoded the page. Your tasks are:

  1. Count the number of div tags within the web_page.
  2. Count the number of images (img) within the web_page.

To identify these elements, search for the strings "<div" and "<img", as elements may include attributes.

Solution

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

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 11
# 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

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt