Skip to content

How to Build an Amazon Web Scraper in Python: An In-Depth Guide for 2023

E-commerce is growing rapidly, with global sales expected to reach $7.4 trillion by 2025 [1]. Amazon leads this charge, capturing over 40% market share in major countries like the US [2].

Tapping into Amazon‘s data can provide invaluable insights for your business. Customer reviews reveal product sentiment. Pricing data enables competitive intelligence. Keyword ranking helps optimize SEO.

In this comprehensive 4500+ word guide, I‘ll share how to extract these e-commerce insights by building a robust Amazon web scraper in Python.

Why Scrape Amazon Product Data?

Here are some examples of how scraped Amazon data can be leveraged:

  • Competitor price monitoring – Track prices of rival products to stay competitive
  • Product research – Analyze reviews and ratings to create better products
  • Dropshipping – Automatically import product details into your e-commerce store
  • SEO optimization – Identify high ranking products and steal keywords
  • Market sizing – Estimate market size and demand from Amazon‘s sales data
  • Lead generation – Enrich prospect profiles with their Amazon purchase history

Web scraping unlocks all kinds of creative applications across sales, marketing, product design and more.

Overview of Scraping Amazon with Python

At a high level, the steps to build an Amazon scraper are:

  1. Send requests to Amazon pages
  2. Parse the HTML response with BeautifulSoup
  3. Extract data like title, price, rating etc. using CSS selectors
  4. Handle pagination and crawl additional pages
  5. Use proxies and random headers to avoid blocks
  6. Export scraped data to CSV/JSON

We‘ll use the requests and beautifulsoup4 libraries in Python for scraping. Pandas will be used for data exporting.

Now let‘s dive into each of these steps in detail.

Setting up the Python Environment

It‘s recommended to use a virtual environment for your web scraping projects.

Here are the steps to set one up:

# Create project directory
mkdir amazon-scraper
cd amazon-scraper

# Create virtual env 
python3 -m venv .env  

# Activate virtual env
source .env/bin/activate  # Linux/macOS
.env\Scripts\activate # Windows

# Install dependencies
pip install requests beautifulsoup4 pandas

This creates an isolated environment for our scraper with the required packages.

Now create a file called to hold the code.

Let‘s start by importing the libraries we‘ll need:


from bs4 import BeautifulSoup
import requests
import json

Time to start scraping!

Scraping a Single Product Page

Let‘s start by scraping a single product page on Amazon.

We‘ll extract key details like:

  • Title
  • Price
  • Rating
  • Number of reviews
  • Product images
  • Description
  • Available variants like size, color etc.

Here‘s how to scrape these fields from an Amazon product page:

import requests 
from bs4 import BeautifulSoup

url = ‘‘

headers = {
    ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3‘

page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, ‘html.parser‘)

title = soup.find(id="productTitle").get_text().strip()
price = soup.find(id="priceblock_ourprice").get_text()
rating = soup.find(id="acrPopover").get(‘title‘).split()[0] 
num_reviews = soup.find(id=‘acrCustomerReviewText‘).get_text() 

# And so on...


A few things to note:

  • We use a mock User-Agent string in headers to mimic a real browser
  • BeautifulSoup parses the HTML content
  • We find elements by ID, class name etc. and extract text or attributes

This gives us a basic scraper to extract details from an Amazon product page.

Let‘s look at how to expand it.

Scraping Multiple Images

Products can have multiple images. We can find them with:

image_elements = soup.find_all(‘img‘, {‘id‘: ‘landingImage‘})
image_urls = [img.get(‘data-old-hires‘) for img in image_elements] 


This first finds all the <img> tags with ID landingImage, then extracts the product image URLs from the data-old-hires attribute.

Extracting Product Variants

To get the available variants like different sizes or colors:

variants = []

# Find all dropdowns 
for dropdown in soup.find_all(‘select‘, {‘id‘: ‘variation_color_name‘}):

  # Extract options
  options = dropdown.find_all(‘option‘)

  for option in options:


This loops through the <select> dropdowns, grabs the <option> elements, and extracts the text into a list.

Getting the Description

The product description is contained in a <div> tag:

description = soup.select_one(‘#productDescription‘).get_text().strip()

This uses CSS selectors to identify the description <div> and return its text.

Assembling Product Details

Now that we can extract each field, let‘s put them together into a JSON structure:

product = {
  ‘title‘: title,
  ‘price‘: price,
  ‘rating‘: rating,
  ‘num_reviews‘: num_reviews,  
  ‘description‘: description,
  ‘images‘: image_urls,
  ‘variants‘: variants

print(json.dumps(product, indent=2))

This gives us a nicely formatted JSON output containing all the key product details!

With just 30 lines of code, we have a scraper that can extract tons of useful data from any Amazon product page.

Next let‘s look at scraping Amazon search results.

Scraping Search Results

To extract data for hundreds of products, we need to scrape search results pages.

For example, let‘s get results for a search query like "laptops":

search_url = ‘‘

page = requests.get(search_url, headers=headers)   
soup = BeautifulSoup(page.content, ‘html.parser‘)

results = []

for product in‘[data-component-type="s-search-result"]‘):

  title = product.h2.text
  url = f"{product.h2.a[‘href‘]}"

    ‘title‘: title,
    ‘url‘: url


This loops through all the search result containers, extracts the product title and URL, and stores them in a list.

We can pass each URL into the scrape_product_page() method to also get the price, rating etc. for each laptop.

Now let‘s look at handling pagination…

Scraping Multiple Pages of Results

To extract more than 10-20 search results, we need to scrape across multiple pages.

The Next Page button can be found using:

next_page = soup.find(‘li‘, {‘class‘: ‘a-disabled‘}) 

if next_page:
  next_url = f"{next_page.a[‘href‘]}"

We can then call the scrape function recursively:

def scrape_search_results(url):

  # Scrape page
  page = requests.get(url, headers=headers)   
  soup = BeautifulSoup(page.content, ‘html.parser‘)

  # Extract products
  products = [] 
  for product in‘[data-component-type="s-search-result"]‘):
    title = product.h2.text
    url = f"{product.h2.a[‘href‘]}"     
    products.append({‘title‘: title, ‘url‘: url})

  # Check for next page
  next_page = soup.find(‘li‘, {‘class‘: ‘a-disabled‘})
  if next_page:
    next_url = f"{next_page.a[‘href‘]}"

  return products

results = scrape_search_results(search_url) 

This continues scraping until there are no more pages left. We can extract 100s of products across pages this way.

Now let‘s look at handling blocks…

Avoiding Blocks with Proxies and User-Agents

To scrape Amazon sustainably, we need proxies and random User-Agents.

Proxies help mask requests so they don‘t appear to all come from one IP address. We can pick a random proxy for each request:

import random

proxies = [
  {‘http‘: ‘‘},
  {‘http‘: ‘‘},
  {‘http‘: ‘‘}  

# Pick a random proxy
proxy = random.choice(proxies)

page = requests.get(url, headers=headers, proxies=proxy) 

User-Agents can be randomized to simulate different browsers/devices:

user_agents = [
  ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36‘,
  ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36‘,
  ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36‘

user_agent = random.choice(user_agents)
headers = {‘User-Agent‘: user_agent} 

Randomizing proxies and User-Agents makes your scraper appear more human and avoids IP blocks.

Using Selenium to Bypass Captchas

For heavy scraping, you may encounter Captchas. To solve these, Selenium browser automation can be used:

from selenium import webdriver
from import By

driver = webdriver.Chrome()

# Wait for captcha to load

# Click captcha checkbox  
driver.find_element(By.ID, ‘captcha-checkbox‘).click()

html = driver.page_source
soup = BeautifulSoup(html, ‘html.parser‘)

# Continue scraping...

This loads the page in a real Chrome browser, clicks the "I‘m not a robot" captcha checkbox, then passes the HTML to BeautifulSoup for scraping.

Note: Make sure to use Selenium in moderation, as excessive automation can still get detected.

Next, let‘s look at structuring and saving the scraped data.

Exporting Scraped Data to CSV/JSON

To work with the scraped data, we need to store it in a structured format like CSV or JSON.

For example, we can store each product as a row in CSV format:

import csv

with open(‘products.csv‘, ‘w‘) as csvfile:

  writer = csv.DictWriter(csvfile, fieldnames=[‘title‘, ‘price‘, ‘rating‘]) 

  for product in products:

This allows analyzing the data in Excel or loading it into tools like MySQL, BigQuery etc.

For JSON output, we can serialize the data:

import json

with open(‘products.json‘, ‘w‘) as f:
  json.dump(products, f)  

These exports allow conveniently accessing the scraped data for analysis and consumption by other applications.

Scheduling and Automating the Scraper

For continuous scraping, we can schedule the spider:

  • Run the scraper every night to get daily data
  • Use cron jobs or Windows task scheduler
  • Append new results to existing CSV/JSON files
  • Push data to databases like PostgreSQL for Dashboard visualizations

For example:


import schedule
import time

def run_scraper():
  print("Running scraper...")
  results = scrape_products()

  with open(‘products.csv‘, ‘a‘) as f: 
    writer = csv.writer(f)
    for item in results:


while True:

This schedules the scraper to run daily and append results to the CSV file.

For other languages like Node.js, similar scheduler libraries are available.

This allows building up a rich, up-to-date dataset over time.

Handling Errors and Maximizing Uptime

Robust scrapers need to handle errors gracefully:

  • Use try/except blocks to catch exceptions
  • Implement exponential backoff retries on connection errors
  • Rotate IPs/proxies and random headers when blocked
  • Pause scraping for some time if errors exceed a threshold
  • Save progress periodically to restart on crashes
  • Send email/Slack notifications on errors
  • Expose metrics like pages scraped, errors etc. for monitoring

This results in a resilient scraper that can run 24/7 with minimal supervision.

Here‘s an example of retrying on errors:

import time, random


for page in search_results:

  for retry in range(RETRIES):

      data = scrape_page(page)

    except Exception as e:
      delay = RETRY_DELAY * (2 ** retry)
      print(f"Scrape failed. Retrying in {delay} seconds..")
      time.sleep(delay + random.uniform(0, 3))


  # Failed after 3 retries    
    print(f"Failed to scrape {page}. Skipping.")

This retries up to 3 times with growing delays, then skips failed pages.

Similar techniques should be used for each stage of the scraper like crawling, parsing, exporting etc. to make the system robust.

Scraper Architecture and Best Practices

Some best practices for scraper architecture:

  • Separation of concerns – Split into modules for crawling, parsing, storage etc.
  • Scalability – Make stateless, leverage queues like RabbitMQ or Kafka
  • Extensibility – Allow adding new data sources/formats without much change
  • Debugging aids – Have flags to print output, run single-threaded etc.
  • Instrumentation – Expose metrics like pages crawled, errors etc. for monitoring
  • Deployment – Containerize with Docker for easy distribution and scaling

This results in a well-structured, production-grade system.

For large datasets, distributed scraping on a cluster of servers with Scrapy may be required.

Advanced Techniques

We‘ve covered the foundations. Some advanced techniques include:

  • Asynchronous scraping with aiohttp to scrape pages concurrently and maximize throughput
  • Selenium browser automation when page requires JavaScript rendering
  • OCR with Pytesseract to extract text from images/captchas
  • AWS Lambda for serverless scraping to reduce infra costs
  • Scraper API services like ScraperAPI or ProxyCrawl to simplify deployment

But much can be done with just Requests, BeautifulSoup and clever selectors.

Conclusion and Next Steps

We‘ve seen how to build a robust scraper to extract key data from Amazon product listings and pages.

Some ways to expand on this:

  • Scrape buyer reviews, Q&A tabs and other details
  • Analyze sentiment on products and brands from reviews
  • Enrich your product catalog by importing Amazon data
  • Build a price tracker for competitors‘ products
  • Expand to more Amazon categories like electronics, clothing etc.

The possibilities are endless when you can harvest data at scale from the world‘s largest e-commerce site!

I hope this end-to-end guide gives you a blueprint for extracting value from Amazon. Feel free to reach out if you have any other specific questions.

Happy scraping!


  1. eMarketer, Global Ecommerce Forecast 2022
  2. Statista, Amazon‘s market share across top e-commerce markets worldwide

Join the conversation

Your email address will not be published. Required fields are marked *