amazon-product-search

πŸ›οΈ Amazon Product Search Library πŸ“¦

Overview

Tired of manually browsing Amazon for the best deals? 🌐 Meet Amazon Product Search β€” your trusty Python library to scrape product listings and detail pages from Amazon with just a few lines of code. Powered by curl_cffi browser impersonation (so Amazon’s bot filter doesn’t return a 503 error page) and BeautifulSoup4 (bs4) for parsing, with optional async requests for speed. πŸŽ‰

Key Features

Setup πŸ› οΈ

pip install amazon-product-search-v2

Installs the latest stable release. Package name on PyPI is amazon-product-search-v2.

2. Install via GitHub (For Developers) πŸ¦Έβ€β™‚οΈ

git clone --depth 1 https://github.com/ManojPanda3/amazon-product-search
cd amazon-product-search
pip install -e .
# or with venv: python -m pip install -e .

Usage πŸ“š

Import

from amazon_product_search import (
    Amazon,
    AmazonProduct,
    AmazonProductDetails,
    AmazonResult,
)
# Package is amazon-product-search-v2 on PyPI, but import stays amazon_product_search
from amazon_product_search import Amazon

# Session is reused across searches (keep-alive). Use as context manager to auto-close.
with Amazon() as amazon:
    result = amazon.search("thinkpad", productType="electronics", page=1)
    # or: amazon = Amazon(); result = amazon.search(...); amazon.close()

print(result.current_page, "/", result.total_pages)
for prod in result.products:
    print(prod.title, prod.price, prod.currency)
    print(prod.get())  # dict with title/link/review/review_numbers/currency/price/image

Or one-off:

amazon = Amazon(is_debuging=False, workers=4)
result = amazon.search(productName="iPhone", productType="electronics", brand="Apple", priceRange="80000-100000", page=2)
# result is AmazonResult β€” also iterable/len-compatible
print(len(result))          # == len(result.products)
for product in result:      # iterates products directly
    print(product.get())

Deep-scrape a product page πŸ•΅οΈ

AmazonProduct.deep_scrape() fetches a product detail page and returns an AmazonProductDetails object. Amazon renders different widgets for different products, so the result uses one generalized schema: missing scalars are None, missing lists are [], and missing mappings are {} β€” the keys are always present.

from amazon_product_search import AmazonProduct

product = AmazonProduct(link="https://www.amazon.com/dp/B073CWSQ51")
details = product.deep_scrape()

print(details.title, details.brand, details.rating)
print(details.price, details.price_value, details.currency)
print(details.availability)
print(details.features)
print(details.specifications)   # {"Brand Name": "COLOR WOW", ...}
print(details.product_details)  # {"Item model number": "CW527", ...}
print(details.images)
print(details.variations)       # size/colour options with ASIN + price
print(details.get())            # JSON-serialisable dict

Fields:

Group Fields
Identity asin, title, brand, link, breadcrumbs
Offer price (display string), price_value (float), currency, availability
Social proof rating, review_count, best_sellers_rank
Content features, specifications, product_details
Media main_image, images
Variations variations (dimension, value, asin, selected, available, price, price_value, image, url)

Notes:

Saved HTML and local files are supported, which is handy for tests:

details = product.deep_scrape(html=saved_html)
# or point the product at a file
details = AmazonProduct(link="/path/to/product.html").deep_scrape()

Async Search (overlapping network I/O) 🐍

Searching Amazon is dominated by network wait, so async_search() lets you run several searches at once. It performs one search per call; batch them yourself with asyncio.gather() or a task group.

import asyncio
from amazon_product_search import Amazon

async def main():
    async with Amazon() as amazon:
        results = await asyncio.gather(
            amazon.async_search("thinkpad", productType="electronics"),
            amazon.async_search("macbook air"),
            amazon.async_search("dell xps"),
        )
    for result in results:
        print(result.current_page, len(result.products))

asyncio.run(main())

Notes:

Returns

AmazonResult dataclass:

@dataclass
class AmazonResult:
    products: list[AmazonProduct]
    current_page: int
    total_pages: int
    def get() -> dict: ...  # {products: [...], current_page, total_pages}

Each AmazonProduct:

@dataclass
class AmazonProduct:
    title: str | None
    link: str | None          # https://www.amazon.com/dp/...
    review: str | None        # "4.5 out of 5 stars" (None when missing)
    review_numbers: int | None # 1234 (parentheses stripped; k/m/b suffixes converted)
    price: float | None        # 12.99 (commas stripped)
    currency: str | None      # "$" / "β‚Ή" etc. (split on \u00a0)
    image: str | None
    def get() -> dict: ...

Backward compat: for p in result, len(result), result[i] all proxy to result.products.

AmazonProduct.deep_scrape() returns AmazonProductDetails:

@dataclass
class AmazonProductDetails:
    asin: str | None
    title: str | None
    brand: str | None
    link: str | None
    breadcrumbs: list[str]
    price: str | None          # display text, e.g. "$1,299.99"
    price_value: float | None  # parsed numeric value
    currency: str | None
    availability: str | None
    rating: float | None
    review_count: int | None
    best_sellers_rank: list[str]
    features: list[str]
    specifications: dict[str, str]
    product_details: dict[str, str]
    main_image: str | None
    images: list[str]
    variations: list[dict]
    def get() -> dict: ...     # same keys for every product

Examples

Paginated search (new syntax)

from amazon_product_search import Amazon
import json

with Amazon() as amazon:
    # Page 1
    r1 = amazon.search("thinkpad", productType="electronics", page=1)
    print(f"Page {r1.current_page} of {r1.total_pages} β€” {len(r1)} products")
    # Page 2
    r2 = amazon.search("thinkpad", productType="electronics", page=2)
    print(json.dumps(r2.get(), indent=2))

Iterate all pages

amazon = Amazon()
page = 1
all_products = []
while True:
    res = amazon.search("laptop", page=page)
    all_products.extend(res.products)
    if res.current_page >= res.total_pages:
        break
    page += 1
print(f"Collected {len(all_products)} across {res.total_pages} pages")
amazon.close()

Old-style loop (still works)

res = amazon.search("iPhone")
for product in res:  # or res.products
    d = product.get()
    print(f"Title: {d['title']}")
    print(f"Price: {d['currency']}{d['price']}")
    print(f"Review: {d['review']} ({d['review_numbers']})")
    print(f"Link: {d['link']}")
    print("-" * 40)

How It Works πŸ”

  1. URL building: urllib.parse.urlencode safely encodes k, i, brand, price, page.
  2. Request: curl_cffi with browser impersonation (real TLS/HTTP2 fingerprint), session reuse (keep-alive), 20s timeout. A blocked attempt is retried under a different browser fingerprint with exponential backoff.
  3. Parse products: SoupStrainer("div", {"data-component-type":"s-search-result"}) + lxml β€” only product divs are parsed.
  4. Parse pagination: SoupStrainer("div", {"data-csa-c-content-id":"pagination-button"}) β†’ reads span.s-pagination-selected (current) and max a/span.s-pagination-item numeric (total, e.g. 260).
  5. Extract: runs extract_data (title/link/review/price/image) per div. Extraction is single-threaded on purpose: parsing holds the GIL, and a thread pool measured slower than a plain loop.
  6. Return: AmazonResult(products, current_page, total_pages).

Deep-scrape pipeline

AmazonProduct.deep_scrape(html=None) handles the much larger, variable product-detail document separately:

  1. Source: explicit html= wins. Otherwise link is read from disk when it is a file, or fetched once with curl_cffi (chrome124, 20s timeout, redirects, Accept-Language) and checked with raise_for_status().
  2. Base URL: an HTTP(S) link is kept; otherwise the canonical/OG URL is used, falling back to https://www.amazon.com, so relative image and variation URLs become absolute.
  3. Sections: identity (title, ASIN, brand, breadcrumbs), offer (price/currency/availability), social proof (#acrPopover, review count), content (#feature-bullets, detail bullets, spec tables), media (#landingImage + gallery JSON), and variations (desktop-twister-sort-filter-data).
  4. Projection: always returns AmazonProductDetails with every key present. Missing sections become None, [], or {}; absent markup never raises. price keeps Amazon’s display text while price_value is numeric.
  5. Return: .get() is an asdict() view, ready for json.dumps().

Unlike search(), deep_scrape() makes a single direct request and does not go through Transport’s fingerprint rotation/retry loop.

What’s New in v0.1.3

What’s New in v0.1.2

Anti-bot transport πŸ›‘οΈ

Amazon blocks scrapers on the TLS/HTTP2 fingerprint, not just headers. A plain requests/httpx call gets a 503 β€œSorry! Something went wrong!” page (or a 200 interstitial containing bm-verify) that parses to zero products. This library uses curl_cffi to impersonate a real browser, which is the fix.

On top of impersonation it:

Tunable via the constructor:

Amazon(timeout=20, max_attempts=4, backoff=0.4)

max_attempts is capped at the number of profiles (6). When all attempts fail you get ValueError: Error while geting data from Amazon; TransportError is exported if you want to catch the underlying cause.

Important Notes ⚠️

Troubleshooting πŸ› οΈ

  1. ValueError: Error product Name is required β€” provide productName.
  2. ValueError: Error while geting data from Amazon β€” every attempt was blocked or errored. Run with Amazon(is_debuging=True) to see which fingerprint was rejected per attempt, then try Amazon(max_attempts=6) for a wider rotation.
  3. Empty products β€” no match or HTML changed; check pagination (total_pages) and try different page.
  4. None fields β€” normal (Amazon varies per product).
  5. ModuleNotFoundError: No module named 'amazon_product_search' β€” pip install amazon-product-search-v2 in correct venv.
  6. ValueError: link is not valid from deep_scrape() β€” pass a product URL, a readable local HTML file path, or html=....
  7. deep_scrape() returns mostly None / empty fields β€” the page may be a bot challenge, an unavailable product, or saved HTML without detail markup. Compare against docs/SELECTORS.md.
  8. price is None but currency is set β€” Amazon rendered a currency symbol but no buy-box price for that location/session; price_value is also None.

Contributing 🀝

PRs welcome! Open an issue or submit a pull request.

License πŸ“œ

MIT β€” see LICENSE.