A production-style Python scraper that crawls an online shop's catalogue, including pagination and product detail pages. It exports clean data to CSV, JSON, Excel and SQLite and can track price and stock changes between runs.
The demo target is books.toscrape.com, a sandbox shop built for scraping practice. The same structure applies to real shops, marketplaces, directories and listing sites.
$ python -m scraper --details --formats csv,json,xlsx,sqlite
page 1: 20 products (total 20)
...
details 1000/1000
1000 products in 162.2 s (1050 requests)
-> output/products.csv
-> output/products.json
-> output/products.xlsx
-> output/products.db
Changes since last run: 1000 new, 0 price/stock changes
On later runs the SQLite export reports what changed. The demo shop never changes its prices, so this output is illustrative:
Changes since last run: 0 new, 2 price/stock changes
PRICE 51.77 -> 47.99 A Light in the Attic
STOCK sold out Tipping the Velvet
The run above collected all 1,000 products, 50 categories and 12 fields per product, with zero failed requests.
| Field | Example |
|---|---|
| title | A Light in the Attic |
| price / currency | 51.77 / GBP |
| rating | 3 |
| in_stock / stock_count | True / 22 |
| category | Poetry |
| upc | a897fe39b1053632 |
| description, image_url, url, scraped_at | … |
Sample outputs: docs/sample_products.csv · docs/sample_travel.xlsx
- Full crawl – follows pagination, optional detail pages fetched in parallel (thread pool)
- Polite and robust – rate limit shared across threads, retries with exponential backoff on 429/5xx,
Retry-Aftersupport, robots.txt check, identifiable User-Agent, timeouts - Fault tolerant – one broken page is logged and skipped, the run continues
- Clean data – typed fields, robust price parsing (currency symbols, thousands separators, encoding glitches)
- Exports
- CSV (UTF-8 with BOM, opens cleanly in Excel)
- JSON
- Excel with a formatted products sheet (header style, filters, frozen header, column widths) and a summary sheet per category
- SQLite with upsert and a
price_historytable
- Change detection – run it daily (cron / Task Scheduler) and it reports new products, price changes and stock changes
- Category filter –
--category travel - Tested – 12 offline tests against saved HTML pages and a fake HTTP client
pip install -r requirements.txt
python -m scraper # all pages, listing data -> output/products.csv
python -m scraper --pages 2 --details --formats csv,xlsx
python -m scraper --category travel --details --formats xlsx --out output/travel
python -m scraper --details --formats sqlite # schedule daily for price monitoring
python -m pytest # run tests| Option | Default | |
|---|---|---|
--pages N |
all | limit listing pages |
--details |
off | fetch each product page (category, stock count, UPC, description) |
--formats |
csv |
any of csv,json,xlsx,sqlite |
--out |
output/products |
output path without extension |
--delay |
0.3 |
seconds between requests |
--workers |
4 |
parallel detail-page downloads |
scraper/
client.py HTTP session: retries, rate limit, robots.txt
parsers.py pure HTML -> data functions (unit-tested)
core.py crawl orchestration, pagination, parallel details
exporters.py CSV / JSON / Excel / SQLite + change detection
models.py Product dataclass
__main__.py CLI
tests/ pytest suite + saved HTML fixtures
Adapting it to another site usually only means writing new functions in parsers.py.
Only scrape data you are allowed to collect. Respect the site's terms and robots.txt, keep request rates low, and don't collect personal data without a legal basis.
I build custom scrapers, price monitors, lead lists and data pipelines in Python, delivered as CSV, Excel, Google Sheets, a database or an API, with scheduling and alerts.
MIT © TimIng0497