#! /usr/bin/env python3 import requests from bs4 import BeautifulSoup # Step 1: Fetch the HTML content from a URL url = 'http://quotes.toscrape.com/' # A website for learning web scraping try: response = requests.get(url) response.raise_for_status() # Check for bad request html_content = response.text except requests.exceptions.RequestException as e: print(f"Error fetching URL: {e}") exit() # Step 2: Parse the HTML content using Beautiful Soup soup = BeautifulSoup(html_content, 'html.parser') # Step 3: Extract data # Extract the page title page_title = soup.find('h1').text.strip() print(f"Page Title: {page_title}\n") # Extract all quotes and authors using CSS selectors or find_all # Quotes are in tags with class 'text' quotes = soup.find_all('span', class_='text') # Authors are in tags with class 'author' authors = soup.find_all('small', class_='author') print("--- Extracted Quotes and Authors ---") for i in range(len(quotes)): print(f"Quote: {quotes[i].text.strip()}") print(f"Author: {authors[i].text.strip()}\n") # You can also find specific elements by ID results_container = soup.find(id="quotes_content") if results_container: print(f"\nFound element with ID 'quotes_content': {results_container.h2.text.strip()}")