DEV Community

mzulfaas
mzulfaas

Posted on Originally published at smartkitreviewed.com

Extracting Amazon Appstore Listings with Header Coherence (Python)

Most web scraping tutorials still teach a pattern from 2018: pick a list of random User-Agent strings, pick one per request, and sleep for a random interval.

On modern edge CDNs, this pattern triggers rate limits and CAPTCHAs quickly. Edge networks inspect header coherence and TLS fingerprints. If your User-Agent says Chrome 122 on macOS, but your request lacks Sec-Ch-Ua headers or sends conflicting platform hints, the edge drops or challenges the request.

Here is how to structure a lightweight, zero-dependency Python scraper that extracts public listings from the Amazon Appstore without triggering immediate challenges.

1. The Core Problem: Header Incoherence

When a desktop Chrome browser requests a web page, it sends a specific set of HTTP Client Hints (defined in RFC 8942):

  • Sec-Ch-Ua: Browser brand and major version
  • Sec-Ch-Ua-Mobile: Mobile flag (?0 for desktop)
  • Sec-Ch-Ua-Platform: Operating system ("macOS", "Windows", "Linux")
  • Sec-Fetch-Dest: Resource destination (document)
  • Sec-Fetch-Mode: Navigation mode (navigate)
  • Sec-Fetch-Site: Origin relationship (same-origin or none)

If you rotate your User-Agent to an Android phone while sending desktop fetch metadata, the request is flagged as inconsistent.

The Coherent Header Set

def get_coherent_headers():
    return {
        "User-Agent": (
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
            "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
        ),
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
        "Accept-Language": "en-US,en;q=0.9",
        "Sec-Ch-Ua": '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
        "Sec-Ch-Ua-Mobile": "?0",
        "Sec-Ch-Ua-Platform": '"macOS"',
        "Sec-Fetch-Dest": "document",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-Site": "same-origin",
        "Sec-Fetch-User": "?1",
        "Upgrade-Insecure-Requests": "1"
    }
Enter fullscreen mode Exit fullscreen mode

Keep your client hints aligned with your User-Agent string. If you change the browser version in the User-Agent, update Sec-Ch-Ua to match.

2. In-Memory Deduplication

When paging through catalog search results, items frequently shift across pagination boundaries. Storing seen IDs in a Python set prevents duplicate rows without database overhead:

seen_asins = set()
records = []

for asin, title in raw_items:
    if asin in seen_asins or asin == "0000000000":
        continue
    seen_asins.add(asin)
    records.append({
        "asin": asin,
        "app_name": title.strip(),
        "app_url": f"https://www.amazon.com/dp/{asin}"
    })
Enter fullscreen mode Exit fullscreen mode

3. Clean CSV Export (UTF-8 with BOM)

A common issue with scraped data is character encoding corruption when opening CSV files in Microsoft Excel (e.g., apostrophes turning into ’).

Use Python's utf-8-sig encoding. It prepends the UTF-8 Byte Order Mark (BOM) so Excel detects the character set correctly:

import csv

fields = ["asin", "app_name", "category", "price", "rating", "rating_count", "app_url"]

with open("amazon_apps.csv", "w", encoding="utf-8-sig", newline="") as f:
    writer = csv.DictWriter(f, fieldnames=fields)
    writer.writeheader()
    for row in records:
        writer.writerow({k: row.get(k, "") for k in fields})
Enter fullscreen mode Exit fullscreen mode

4. Full Minimal Script

A complete standalone implementation using only the Python standard library is available in the GitHub repository:
mzulfaas/amazon-appstore-scraper

You can run it directly:

python3 scripts/scrape_amazon_appstore.py --query "utilities" --max-results 50
Enter fullscreen mode Exit fullscreen mode

The script extracts the app title, ASIN, developer, rating, review count, and store URL, exporting clean CSV and JSON files in under 2 seconds.

Top comments (0)