A production-style web scraping pipeline that scrapes 60 pages of AliExpress laptop listings, validates and enriches the data with discount metrics and price bands, and appends only new records to a PostgreSQL warehouse. Modular, idempotent at the load layer, observable, and ready to rerun.
E-commerce pages are not built to be scraped. They lazy-load content, show cookie banners on every visit, detect automated browsers, and change their HTML layout without warning. The data you eventually pull is raw text full of inconsistencies. A pipeline that works once is easy. A pipeline that works repeatedly, against a live site, is the harder problem.
The pipeline runs end to end with a single command. Each stage is an isolated module that can also run independently. A DataFrame is passed in memory between extract, transform, and load (no intermediate file unless requested), keeping the pipeline fast and the failure surface predictable.
# Run the full pipeline end-to-end python -m etl.run_all # What run_all.py executes: 1. scraper.health_check() # → HTTP request + real browser test 2. extract.scrape() # → 60 pages, returns DataFrame 3. transform.clean(df) # → type cast, dedupe, engineer features 4. load.append(df) # → dedupe vs DB, append only new rows # Every step: timed, logged via custom ColorFormatter, fail-fast
The extraction layer is the most fragile part of any web scraping pipeline. I treated it as a system with explicit reliability requirements, not a script to be rerun until it works.
Before scraping, scraper.py sends an HTTP request to AliExpress and opens a real Chrome browser. If either fails, the pipeline aborts with a clear error instead of producing garbage output.
Selenium is configured to disable common automation flags. The browser opens looking like a real user agent, not a default Chromedriver instance.
The scraper waits for the cookie banner to render, dismisses it, then continues. No human intervention, no scraped HTML hidden behind a modal.
Every page transition uses WebDriverWait against a specific element selector. No time.sleep() guesses. The scraper waits exactly as long as needed and no longer.
Selenium handles navigation. BeautifulSoup handles parsing. Each tool does what it is best at, and the parsing logic is unit-testable without spinning up a browser.
The scraper walks through all 60 pages of laptop listings in a single run, with retry logic at each page transition. Failure on page 42 does not corrupt the data from pages 1 through 41.
The transform stage does more than clean data. It engineers business-friendly features that turn raw scraped text into something a BI tool or SQL query can answer questions against.
| source column | description |
|---|---|
| product_name | Full listing title |
| product_price | Current sale price (£) |
| was_price | Original crossed-out price (£) |
| discount_info | Discount label from the listing |
| extra_discount | Additional coupon or offer |
| shipping_status | Free shipping or not |
| total_sold_count | Number of units sold |
| top_selling_status | Top selling on AliExpress flag |
| scraped_at | Timestamp of scrape |
| engineered feature | logic |
|---|---|
| discount_amount | was_price − product_price, in £ |
| discount_percentage | % saved off the original price |
| is_on_sale | Boolean · was-price exists and is higher than current price |
| is_free_shipping | Boolean · free shipping offered on the listing |
| is_top_seller | Boolean · top-selling badge present on the listing |
| price_band | Budget / Mid-Range / Upper-Mid / Premium / Luxury |
-- Which laptops offer the best discount? SELECT product_name, product_price, discount_percentage FROM laptops WHERE is_on_sale = TRUE ORDER BY discount_percentage DESC LIMIT 10; -- What price band has the most listings? SELECT price_band, COUNT(*) AS total FROM laptops GROUP BY price_band ORDER BY total DESC;
Selenium handles dynamic JavaScript rendering and navigation. BeautifulSoup handles parsing the resulting HTML. Each tool stays in its lane. The parsing layer is unit-testable without launching a browser, which is the difference between an hour of debugging and ten seconds of pytest.
Unlike PayFlow and ChocoDelight, which wipe and reload, this pipeline appends. Scraping a live source is non-idempotent at the source layer (data changes between runs), so the load step deduplicates against existing rows before inserting. New listings get added, old data stays untouched, no row is ever duplicated.
Extract, transform, and load pass a DataFrame directly between stages instead of always writing CSVs to disk between steps. This keeps the orchestrated pipeline fast. Each step can still be run independently and writes its CSV output if you need to inspect intermediate state.
time.sleep() is a guess. WebDriverWait is a contract. Every page interaction waits for a specific element to be present or interactable, not for a hardcoded number of seconds. Faster on fast networks, more reliable on slow ones, never flaky for reasons that shouldn't matter.
Before extract.py runs, scraper.py confirms AliExpress is reachable via HTTP and Chrome can launch successfully. If either fails, the pipeline aborts before scraping a single page. Saves time on broken environments and surfaces config issues clearly.
Discount percentage, price bands, and boolean flags are computed in the transform stage in pandas, not as derived columns in SQL views. Logic lives in version-controlled Python where it can be tested, reviewed, and changed in one place.
Each engineered feature directly enables a category of analytical question. Below are real business questions a price-intelligence team or competitive-analysis team would ask of this data, all answerable with a single SQL query.
For higher volumes and reliability the natural evolutions would be: Playwright instead of Selenium for faster, more resilient browser automation; proxy rotation to avoid rate-limiting on long-running scrapes; asynchronous fetching (asyncio + aiohttp) for static endpoints to parallelise page retrieval; Airflow or Prefect orchestration for scheduled runs with retry and alerting; change-detection logic to flag price drops or stock changes per listing rather than just appending new rows; and migration tooling (Alembic, Flyway) for evolving the schema safely as scraped attributes change.
I'm currently looking for Data Engineer / Analytics Engineer roles. UK-based, open to remote or hybrid.
A 1M-row PySpark pipeline for synthetic banking transactions, with a validated star schema warehouse, deterministic SHA-256 surrogate keys, and idempotent re-runs on PostgreSQL.
read-case-studyA production-style ETL pipeline that loads 9 raw Brazilian e-commerce CSVs into a full PostgreSQL star schema, with idempotent loads and structured logging.
read-case-study