This code fetches the content of a webpage and parses it into a BeautifulSoup object. You can then use BeautifulSoup methods to navigate and extract elements from the HTML.
Once you have the HTML content, you can extract specific elements using find()
and find_all()
methods:
title = soup.find('title')
print("Page title:", title.string)
links = soup.find_all('a')
for link in links:
print("Link:", link.get('href'))
This code finds the <title>
tag and all <a>
tags (links) in the HTML.
Write a program that scrapes a webpage and extracts all the images (links to <img>
tags) on that page.
Answers: The requests
module is used to make HTTP requests. You can parse HTML with BeautifulSoup()
and specify the parser, such as html.parser
.