Automating Amazon Price Tracking with Python
Tags: webscraping, python, beginners, programming
Published: 2023-04-11 12:00:00

Are you a savvy shopper who is always on the lookout for the best deals on Amazon? Do you find yourself constantly checking the prices of your favorite products to see if they have gone down? If so, you might want to consider using [Python](https://www.python.org/) to automate the process of tracking Amazon prices. In this post, we will show you how to use Python to scrape [Amazon.com](https://www.amazon.com/) for product prices and automate the process of checking out.

### Step 1: Web Scraping

The first step in automating Amazon price tracking with Python is to scrape the product pages of Amazon.com for the desired product. To do this, you can use a web scraping library like [BeautifulSoup](https://beautiful-soup-4.readthedocs.io/en/latest/) or [Scrapy](https://scrapy.org/). In the following example, we will use BeautifulSoup to scrape the product page for a MacBook Pro on Amazon.com:

```python
import requests
from bs4 import BeautifulSoup

def get_price(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"}
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.content, 'html.parser')
    price = soup.find('span', {'class': 'a-offscreen'}).get_text().strip()
    return float(price.replace(',', '').replace('$', ''))

product_url = 'https://www.amazon.com/dp/B08L5VJYWS'
product_price = get_price(product_url)

print(product_price)
```
In this example, the `get_price()` function takes the URL of a product page on Amazon.com as input, scrapes the page for the price of the product, and returns it as a floating-point number. The headers parameter in the `requests.get()` function is used to simulate a web browser to bypass any anti-scraping mechanisms on Amazon.com.

### Step 2: Price Comparison

Once you have scraped the price of a product from Amazon.com, you can compare it with the prices of the same product on different pages or from different sellers. For example, you can check the prices of the same MacBook Pro from different sellers on Amazon.com or compare the prices of the MacBook Pro on Amazon.com with those on other websites like Best Buy or Walmart.

### Step 3: Automation

Now that you can scrape product prices and compare them, you can take the automation to the next level by using an automation library like [Selenium](https://selenium-python.readthedocs.io/) to automate the process of adding the product to the cart and checking out. Here’s an example of how to use Selenium to automate the checkout process on Amazon.com:

```python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Chrome()
browser.get('https://www.amazon.com/')
search_box = browser.find_element(By.ID, 'twotabsearchtextbox')
search_box.send_keys('MacBook Pro')
search_box.send_keys(Keys.RETURN)

product_link = browser.find_element(By.XPATH, '//a[contains(@href, "B08L5VJYWS")]')
product_link.click()

add_to_cart_button = browser.find_element(By.ID, 'add-to-cart-button')
add_to_cart_button.click()

proceed_to_checkout_button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'hlb-ptc-btn-native')))
proceed_to_checkout_button.click()

sign_in_button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'signInSubmit')))
sign_in_button.click()

email_input = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'ap_email')))
email_input.send_keys('your_amazon_email@example.com')

continue_button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'continue')))
continue_button.click()

password_input = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'ap_password')))
password_input.send_keys('your_amazon_password')

sign_in_button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'signInSubmit')))
sign_in_button.click()

shipping_address_button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'address-book-entry-0')))
shipping_address_button.click()

continue_button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.NAME, 'proceedToCheckout')))
continue_button.click()

place_your_order_button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.NAME, 'placeYourOrder1')))
place_your_order_button.click()

confirmation_message = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@class="a-box a-alert-success"]/div')))
print(confirmation_message.text)
```

This code automates the process of searching for a MacBook Pro on Amazon.com, adding it to the cart, signing in to your Amazon account, filling out the shipping address, and placing the order. Once the order is placed, it prints the confirmation message on the screen.

Keep in mind that Amazon may have anti-automation mechanisms in place, and using this code may be against their terms of service. Always use automation tools responsibly and with caution.