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

book
Challenge: Count Number of Images

Oppgave

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.

Løsning

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 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(___.___("___"))
toggle bottom row
some-alt