From cad28329abcc5df8276770e71553d9fc09185aad Mon Sep 17 00:00:00 2001 From: Rakesh Date: Wed, 17 Dec 2025 16:46:20 +0530 Subject: [PATCH 1/7] Define QuotesbotItem fields for quote scraping Add text, author, and tags fields to QuotesbotItem to properly structure the scraped quote data. This makes the item definition more explicit and useful for all spiders in the project. --- quotesbot/items.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/quotesbot/items.py b/quotesbot/items.py index 63a00a9..2bbe8a2 100644 --- a/quotesbot/items.py +++ b/quotesbot/items.py @@ -9,6 +9,6 @@ class QuotesbotItem(scrapy.Item): - # define the fields for your item here like: - # name = scrapy.Field() - pass + text = scrapy.Field() + author = scrapy.Field() + tags = scrapy.Field() From bded20afd55b7232b015ba8b7bd16e5e6315eef3 Mon Sep 17 00:00:00 2001 From: Rakesh Date: Wed, 17 Dec 2025 16:46:43 +0530 Subject: [PATCH 2/7] Add spiders for modern web scraping techniques Add 6 new spiders to demonstrate various modern web scraping scenarios: - toscrape-js: Extract data from JavaScript-rendered content by parsing embedded JSON data in script tags - toscrape-scroll: Handle infinite scroll pages using the API endpoint with JSON responses - toscrape-login: Demonstrate form-based authentication with CSRF token handling using FormRequest.from_response - toscrape-table: Scrape data from table layouts by selecting table rows and cells - toscrape-viewstate: Handle ASP.NET ViewState forms commonly found in legacy enterprise applications - toscrape-random: Scrape single random quote endpoint These spiders provide practical examples for students learning to handle the challenges of modern websites beyond basic HTML parsing. --- quotesbot/spiders/toscrape-js.py | 34 +++++++++++++++++++++ quotesbot/spiders/toscrape-login.py | 34 +++++++++++++++++++++ quotesbot/spiders/toscrape-random.py | 17 +++++++++++ quotesbot/spiders/toscrape-scroll.py | 23 ++++++++++++++ quotesbot/spiders/toscrape-table.py | 40 +++++++++++++++++++++++++ quotesbot/spiders/toscrape-viewstate.py | 40 +++++++++++++++++++++++++ 6 files changed, 188 insertions(+) create mode 100644 quotesbot/spiders/toscrape-js.py create mode 100644 quotesbot/spiders/toscrape-login.py create mode 100644 quotesbot/spiders/toscrape-random.py create mode 100644 quotesbot/spiders/toscrape-scroll.py create mode 100644 quotesbot/spiders/toscrape-table.py create mode 100644 quotesbot/spiders/toscrape-viewstate.py diff --git a/quotesbot/spiders/toscrape-js.py b/quotesbot/spiders/toscrape-js.py new file mode 100644 index 0000000..bc56284 --- /dev/null +++ b/quotesbot/spiders/toscrape-js.py @@ -0,0 +1,34 @@ +import json +import re +import scrapy +from quotesbot.items import QuotesbotItem + +class ToScrapeJSSpider(scrapy.Spider): + name = "toscrape-js" + start_urls = ['http://quotes.toscrape.com/js/'] + + def parse(self, response): + script_data = response.xpath('//script[contains(text(), "var data =")]/text()').get() + if script_data: + # Extract the JSON list from the script text + json_str = re.search(r'var data = (\[.*?\]);', script_data, re.DOTALL).group(1) + data = json.loads(json_str) + + for quote in data: + yield QuotesbotItem( + text=quote['text'], + author=quote['author']['name'], + tags=quote['tags'] + ) + + # Pagination for JS page usually follows the same pattern or links + # But on the JS page, the "Next" button is also JS generated. + # However, the URL structure /js/page/2/ usually works or we can find the link in the data if present. + # For this example, let's assume we just want to show how to extract data from the script. + # If we want pagination, we might need to check if the script has 'next' info or just increment page number. + # Let's check if there is a next page link in the HTML (often there is a