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

Opgave

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 11
single

single

# 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

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt