Skip to main content

AmazonScraper Python Package Documentation

AmazonScraper is a Python package for scraping product details from Amazon.in using a product's ASIN. It retrieves various pieces of information including the product title, pricing, categories, technical details, additional specifications, ratings, and feature highlights.

Downloads

PyPI Downloads

Table of Contents

Overview

AmazonScraper is designed to extract detailed information from an Amazon.in product page by parsing its HTML. With this package, users can retrieve:

  • Product Title
  • Pricing Details (MRP and Selling Price)
  • Category Tags (Breadcrumbs)
  • Technical and Additional Specifications
  • Detailed Product Information (Bullet Points)
  • Ratings and Review Counts
  • Feature Highlights (About Section)

Features

  • Comprehensive Scraping: Extracts all relevant product data in one go.
  • Robust Error Handling: Gracefully handles missing data or page fetch errors.
  • Structured Data Models: Returns data in easy-to-use Python dataclasses.
  • Customizable HTML Saving: Option to save the prettified HTML for debugging.

Requirements

Install the dependencies via pip:

pip install httpx beautifulsoup4 pydantic fake_useragent

Installation

Clone or download the repository and ensure that the package structure (including the models and utils modules) is maintained. Then, include the package in your project as needed.

Usage

Basic Usage

Below is a sample code snippet to demonstrate how to use the AmazonScraper:

from dibkb_scraper import AmazonScraper
# Initialize the scraper with a valid Amazon ASIN
asin = "B00935MGKK"
scraper = AmazonScraper(asin)

# Retrieve all product details
product_details = scraper.get_all_details()

# Access and print various product attributes
print("Title:", product_details.product.title)
print("MRP:", product_details.product.pricing.mrp)
print("Selling Price:", product_details.product.pricing.selling_price)
print("Categories:", product_details.product.categories)
print("Highlights:", product_details.product.description.highlights)
print("Technical Specs:", product_details.product.specifications.technical)
print("Additional Specs:", product_details.product.specifications.additional)
print("Detail Bullets:", product_details.product.specifications.details)
print("Ratings:", product_details.product.ratings.rating)
print("Review Count:", product_details.product.ratings.review_count)

Saving the HTML

To save the prettified HTML of the product page (useful for debugging), use the page_html_to_text method:

# Saves the HTML content to 'B00935MGKK.txt' (or a custom file name)
scraper.page_html_to_text("B00935MGKK_page")

API Reference

AmazonScraper Class

__init__(self, asin: str)

  • Parameters:
    • asin (str): The Amazon Standard Identification Number of the product.
  • Description: Initializes the scraper, constructs the product URL, sets HTTP headers, and retrieves the HTML content.

page_html_to_text(self, name: Optional[str] = None)

  • Parameters:
    • name (Optional[str]): Optional filename for the output text file. Defaults to the ASIN if not provided.
  • Description: Saves the prettified HTML of the product page into a text file.

get_product_title(self) -> Optional[str]

  • Returns: The product title as a string, or None if not found.
  • Description: Extracts and returns the product title from the page.

get_mrp(self) -> Optional[float]

  • Returns: The Maximum Retail Price (MRP) as a float, or None if not found.
  • Description: Extracts the MRP from the designated HTML element.

get_selling_price(self) -> Optional[float]

  • Returns: The selling price as a float, or None if not found.
  • Description: Retrieves the selling price from the page.

get_tags(self) -> List[str]

  • Returns: A list of category tags (breadcrumbs) as strings.
  • Description: Extracts breadcrumb links that indicate product categories.

get_technical_info(self) -> Dict[str, str]

  • Returns: A dictionary of technical specifications in key-value pairs.
  • Description: Parses the technical details table from the product page.

get_additional_info(self) -> Dict[str, str]

  • Returns: A dictionary containing additional product details.
  • Description: Extracts further details from the secondary details table.

get_product_details(self) -> Dict[str, str]

  • Returns: A dictionary of detailed product information (e.g., bullet points).
  • Description: Retrieves information from the "detail bullets" section of the product page.

get_ratings(self) -> Ratings

  • Returns: A Ratings object containing the product's average rating and the total review count.
  • Description: Extracts rating and review count, with a fallback method if the primary extraction fails.

get_about(self) -> Union[List[str], Dict[str, str]]

  • Returns: A list of product description highlights, or an error dictionary if extraction fails.
  • Description: Retrieves the feature bullets from the "feature-bullets" section.

get_all_details(self) -> AmazonProductResponse

  • Returns: An AmazonProductResponse object that consolidates all scraped product details. If the page fails to load, the response includes an error message.
  • Description: Aggregates all product data into a structured response.

Data Models

The package uses several dataclasses to organize the scraped data:

Ratings

  • Attributes:
    • rating (Optional[float]): The average product rating.
    • review_count (Optional[int]): The total number of reviews.

Pricing

  • Attributes:
    • mrp (Optional[float]): The Maximum Retail Price.
    • selling_price (Optional[float]): The current selling price.

Description

  • Attributes:
    • highlights (List[str]): A list of product highlight points.

Specifications

  • Attributes:
    • technical (Dict[str, str]): Technical specifications from the product page.
    • additional (Dict[str, str]): Additional product details.
    • details (Dict[str, str]): Detailed information extracted from the bullet points.

Product

  • Attributes:
    • title (Optional[str]): The product title.
    • pricing (Pricing): Pricing details of the product.
    • categories (List[str]): Category tags (breadcrumbs).
    • description (Description): Feature highlights.
    • specifications (Specifications): Detailed specifications.
    • ratings (Ratings): Rating information.

AmazonProductResponse

  • Attributes:
    • product (Product): An object containing all the scraped product details.
    • error (Optional[str]): An error message if the scraping process fails.

Error Handling

  • Page Fetch Errors: If the scraper fails to retrieve the page (e.g., due to network issues or an invalid ASIN), the AmazonProductResponse will include an error field.
  • Parsing Exceptions: Individual methods include exception handling to ensure that missing elements do not break the entire scraping process.

License

This project is licensed under the MIT License. See the LICENSE file for full details.

Contributing

Contributions are welcome! Please feel free to open an issue or submit a pull request with your suggestions or improvements.

Disclaimer

This package is provided for educational and research purposes only. Users must comply with Amazon's terms of service and applicable laws when scraping websites. Use the package responsibly.

Metadata

Release files for dibkb-scraper 0.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dibkb-scraper 0.3.4
File Size Uploaded Provenance Actions
dibkb_scraper-0.3.4.tar.gz 14.7 kB Without provenance Details for dibkb_scraper-0.3.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dibkb-scraper 0.3.4
File Interpreter ABI Platform Actions
dibkb_scraper-0.3.4-py3-none-any.whl Python 3 none any Details for dibkb_scraper-0.3.4-py3-none-any.whl

Total release size: 27.7 kB

Release files / dibkb_scraper-0.3.4.tar.gz

Download URL dibkb_scraper-0.3.4.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4a8b29f2acaf9cafc8d09846c9ae6d721cdadf0919e18c475e34c01080cdb4a0
BLAKE2b-256 checksum
How to use checksums
df17593f2fbd15f3592b669592189045bf25df4ce6ab3abb52d8feb4fba40832
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Release files / dibkb_scraper-0.3.4-py3-none-any.whl

Download URL dibkb_scraper-0.3.4-py3-none-any.whl
Size 13.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9e9f2cb09527ec7bdd29f64ff907621eb1d23ca0dd023c417e7a7a0564bd2b24
BLAKE2b-256 checksum
How to use checksums
0e3055fd2fb9c7060062df783ab1ecbc5beee5dcbf9c98229e2f4d23ca7a56d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Security for dibkb-scraper 0.3.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page