Web Scraping with Beautiful Soup in Python

Web scraping is a technique for extracting data from websites. It involves making HTTP requests to web pages and parsing the HTML content to retrieve the desired information. Beautiful Soup is a popular and easy-to-use Python library for web scraping. In this article, we will look at how to use Beautiful Soup for web scraping in Python.
Prerequisites
Section titled “Prerequisites”Before we begin, ensure that you have the following installed on your computer:
- Python 3
- Beautiful Soup 4
- Requests library
You can install Beautiful Soup and Requests using pip:
pip install beautifulsoup4 requestsGetting Started with Beautiful Soup
Section titled “Getting Started with Beautiful Soup”First, let’s import the necessary libraries:
import requestsfrom bs4 import BeautifulSoupNext, we need to make an HTTP request to the target webpage and store the content in a variable. In this example, we will scrape the Wikipedia page for Python programming language.
url = "https://en.wikipedia.org/wiki/Python_(programming_language)"response = requests.get(url)
## Check if the request was successfulif response.status_code == 200: page_content = response.textelse: print(f"Error {response.status_code}: Unable to fetch the webpage.")Now that we have the HTML content, we can create a Beautiful Soup object and parse the HTML:
soup = BeautifulSoup(page_content, "html.parser")Extracting Data with Beautiful Soup
Section titled “Extracting Data with Beautiful Soup”Beautiful Soup provides several methods to search and navigate the HTML tree. Some of the commonly used methods are:
find(): Searches for the first occurrence of a tag that matches the given criteria.find_all(): Returns a list of all tags that match the given criteria.select(): Searches for tags that match the given CSS selector.
Let’s use these methods to extract some information from the Wikipedia page.
Extracting the page title
Section titled “Extracting the page title”title = soup.find("title").textprint(f"Page title: {title}")Extracting all headings
Section titled “Extracting all headings”headings = soup.find_all(["h1", "h2", "h3", "h4", "h5", "h6"])for heading in headings: print(heading.text.strip())Extracting the table of contents
Section titled “Extracting the table of contents”toc = soup.find("div", {"id": "toc"})toc_items = toc.find_all("li")for item in toc_items: print(item.text.strip())Putting It All Together
Section titled “Putting It All Together”Here’s the complete code for our example:
import requestsfrom bs4 import BeautifulSoup
url = "https://en.wikipedia.org/wiki/Python_(programming_language)"response = requests.get(url)
if response.status_code == 200: page_content = response.textelse: print(f"Error {response.status_code}: Unable to fetch the webpage.")
soup = BeautifulSoup(page_content, "html.parser")
title = soup.find("title").textprint(f"Page title: {title}\n")
print("Headings:")headings = soup.find_all(["h1", "h2", "h3", "h4", "h5", "h6"])for heading in headings: print(heading.text.strip())
print("\nTable of contents:")toc = soup.find("div", {"id": "toc"})toc_items = toc.find_all("li")for item in toc_items: print(item.text.strip())Conclusion
Section titled “Conclusion”In this article, we learned how to use Beautiful Soup for web scraping in Python. Beautiful Soup is a powerful and flexible library that makes it easy to extract data from websites. With a few lines of code, you can quickly retrieve the information you need from a webpage.
Remember that web scraping may be subject to the terms of service of the websites you are scraping, as well as legal and ethical considerations. Always respect the website’s robots.txt file, and avoid excessive requests that may cause a burden on the server.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js