Python Zweb
Python lessons
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.
Extracting Specific Elements
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.
Activity
Try It Yourself!
Write a program that scrapes a webpage and extracts all the images (links to <img> tags) on that page.
Quick Quiz
Quick Quiz
- What module is used to make HTTP requests in web scraping?
- How do you parse HTML content using BeautifulSoup?
Answers: The requests module is used to make HTTP requests. You can parse HTML with BeautifulSoup() and specify the parser, such as html.parser.