The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Scrape quotes by category from websites

To scrape quotes by category from websites, you can use Python with BeautifulSoup and requests libraries. Below is a basic script that demonstrates how to scrape quotes by category (like love, life, motivation, etc.) from a site like Quotes to Scrape, which is a popular test site for scraping practice.


✅ Python Script: Scrape Quotes by Category

python
import requests from bs4 import BeautifulSoup BASE_URL = "http://quotes.toscrape.com" CATEGORY = "love" # Change to "life", "inspirational", etc. def scrape_quotes_by_category(category): url = f"{BASE_URL}/tag/{category}/" quotes = [] while url: response = requests.get(url) if response.status_code != 200: print(f"Failed to fetch: {url}") break soup = BeautifulSoup(response.text, "html.parser") for quote_div in soup.select(".quote"): text = quote_div.find("span", class_="text").get_text() author = quote_div.find("small", class_="author").get_text() quotes.append({ "quote": text, "author": author }) next_button = soup.select_one("li.next > a") if next_button: next_page = next_button["href"] url = BASE_URL + next_page else: url = None return quotes # Example usage: quotes = scrape_quotes_by_category(CATEGORY) for q in quotes: print(f"{q['quote']} - {q['author']}")

🔄 Customization Tips:

  • Replace CATEGORY with any available tag like life, inspirational, humor, etc.

  • For more robust scraping or large-scale jobs, add time.sleep() between requests and use fake_useragent or requests.Session() to avoid getting blocked.

  • Save results to .csv, .json, or a database depending on your use case.

Would you like a version that writes the scraped quotes to a file or stores them in a database?

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About