Opens in a new tab
Lesson 5

Web Scraping with Beautiful Soup

Python Mastery Program, Stage 3 Python, Web Scraping

Written by

Muhammad Atif

Last Updated

Introduction to Web Scraping

Web scraping is the process of extracting information from websites by parsing the HTML content. It can be used to gather data from various sources and compile it for analysis or other purposes.

Beautiful Soup Basics

Beautiful Soup is a Python library that makes it easy to parse HTML and XML documents. It provides methods for navigating, searching, and modifying the parse tree.

Example:

Python
from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

soup = BeautifulSoup(html_doc, 'html.parser')

Navigating the HTML Tree

You can navigate the HTML tree using Beautiful Soup’s methods, such as find, find_all, and select.

Example:

Python
# Find the first 'a' tag
first_a_tag = soup.find('a')

# Find all 'a' tags
all_a_tags = soup.find_all('a')

# Find elements using CSS selectors
title = soup.select_one('head > title')

Extracting Data from Websites

Once you have identified the elements you want to extract, you can use Beautiful Soup’s attributes and methods to extract the data.

Example:

Python
# Get the text content of an element
title_text = title.get_text()

# Get the value of an attribute
link = first_a_tag['href']

Hands-on Exercises

  1. Scrape a list of quotes from a webpage and store them in a text file.
  2. Extract a table of data from a webpage and save it as a CSV file.
  3. Scrape product information, such as name and price, from an e-commerce website and save it as a JSON file.

Real-World Example

Web scraping is used in various industries to gather data from different sources. For example, it can be used to collect news articles, product listings, or social media data for analysis and research purposes.

Additional Resources

Summary and Next Steps

In this lesson, you have learned about:

  • Web scraping and its use cases
  • The basics of Beautiful Soup for parsing HTML
  • Navigating the HTML tree using Beautiful Soup methods
  • Extracting data from websites

Muhammad Atif

Data Analyst & Web Development Professional | Focused on Machine Learning, SQL and Python

Transitioned from a background in finance to IT, I now focus on data science and web development, with specialties in Machine Learning, SQL, and Python. My articles aim to clarify complex IT concepts, informed by a rich history in analytical thinking and strategy.

You Might Also Like

Browse more topics and insights handpicked just for you. Stay updated and informed with our latest articles and news.