Web scraping is the process of extracting data from websites. It involves programmatically fetching web pages and then parsing their content to retrieve specific information. This is often done when an official API is not available or doesn't provide the required data.
Beautiful Soup 4 (often referred to as `bs4`) is a powerful Python library specifically designed for parsing HTML and XML documents. It creates a parse tree from the raw HTML/XML content, allowing developers to easily navigate, search, and modify the parse tree. Beautiful Soup sits atop an HTML/XML parser (like `lxml` or `html.parser`), providing Pythonic idioms for iterating, searching, and modifying the parse tree.
Key steps in web scraping using `requests` (for fetching) and `Beautiful Soup` (for parsing):
1. Fetch the HTML content: Use an HTTP library like `requests` to send an HTTP GET request to the target URL and retrieve the raw HTML content of the page.
2. Parse the HTML: Create a `BeautifulSoup` object by passing the raw HTML content and a parser (`'html.parser'` is built-in and generally good for most HTML, while `'lxml'` is faster and more forgiving).
3. Navigate and Search: Use Beautiful Soup's methods (`find()`, `find_all()`, `select()`) to locate specific HTML elements based on their tags, classes, IDs, or other attributes, similar to how CSS selectors work.
4. Extract Data: Once the desired elements are found, extract their text content (`.text`) or attribute values (`['attribute_name']`).
Ethical Considerations: Always be mindful of the website's `robots.txt` file (e.g., `www.example.com/robots.txt`), which specifies rules for web crawlers. Respect rate limits, avoid overwhelming servers with too many requests, and ensure you are not scraping private or copyrighted data without permission. Some websites may also implement measures to detect and block scrapers.
Example Code
import requests
from bs4 import BeautifulSoup
1. Define the URL of the target website
url = "http://quotes.toscrape.com/"
2. Send an HTTP GET request to the URL
response = requests.get(url)
Check if the request was successful (status code 200)
if response.status_code == 200:
3. Parse the HTML content using Beautiful Soup
We use 'html.parser' which is a standard Python library parser
soup = BeautifulSoup(response.text, 'html.parser')
print(f"--- Scraping data from: {url} ---")
4. Find all quote containers on the page
Quotes are typically enclosed in a <div> with class 'quote'
quotes = soup.find_all('div', class_='quote')
5. Iterate through each quote and extract information
for i, quote in enumerate(quotes):
Extract the text of the quote
It's usually within a <span> with class 'text'
text = quote.find('span', class_='text').text
Extract the author of the quote
It's usually within a <small> with class 'author'
author = quote.find('small', class_='author').text
Extract the tags associated with the quote
Tags are within a <div> with class 'tags', then multiple <a> tags with class 'tag'
tags_div = quote.find('div', class_='tags')
tags = [tag.text for tag in tags_div.find_all('a', class_='tag')]
print(f"\n--- Quote {i+1} ---")
print(f"Text: {text}")
print(f"Author: {author}")
print(f"Tags: {', '.join(tags)}")
else:
print(f"Failed to retrieve the webpage. Status code: {response.status_code}")








Web Scraping with Beautiful Soup 4