Ưu đãi giới hạn thời gian dành cho proxy dân dụng:Phiếu giảm giá 1000GB, chỉ 0,79 đô la/GB

Hãy lấy nó ngay bây giờ

icon
icon

Proxy Socks5: Nhận ưu đãi 85% trong thời gian có hạn, tiết kiệm 7650 đô la

Hãy lấy nó ngay bây giờ

icon
icon
logo logo
Home

< Back to blog

How to track Amazon prices using python

Rose . 2024-01-19

In today's e-commerce era, it is very important for consumers to grasp changes in product prices in real time. As the world's largest online retailer, Amazon's product prices change frequently. This article will guide you on how to use Python to track product prices on Amazon so you can buy when the price is right.

1. Preparation work

Install the necessary Python libraries: Before you start, you need to install a few Python libraries, including requests (for sending HTTP requests), BeautifulSoup (for parsing HTML pages), and datetime (for processing dates and times). You can install these libraries via pip using the following command:

shellcopy code

pip install requests beautifulsoup4 datetime

Obtain the source code of the Amazon product page: To obtain the product price information, you need to grab the page source code from the Amazon website. Please make sure you are logged in to your Amazon account and open the product page you want to track. Then, use your browser's developer tools (the shortcut is usually F12) to open the Network Requests tab, find the URL of the product page, and copy the URL.

2. Write Python scripts

Next, we will write a Python script to obtain product price information. Please open your text editor, create a new file, and paste the following code into the file:


import requests

from bs4 import BeautifulSoup

import datetime

# Define the product URL to be tracked

amazon_url = 'Paste the product page URL you copied'

#Send HTTP request to obtain page source code

response = requests.get(amazon_url)

response.raise_for_status() # Check whether the request is successful

# Use BeautifulSoup to parse the page source code

soup = BeautifulSoup(response.text, 'html.parser')

# Position the price element. Depending on the actual situation, you may need to adjust the selector according to the web page structure.

price_tag = soup.select('span.a-offscreen')[0] # Assume that the price element is contained in the span tag and the class name is a-offscreen

price = price_tag.get_text(strip=True) # Extract price text

# Convert prices to floating point format for mathematical operations and comparisons

price_float = float(price.replace('$', ''))

# Get the current date and time, used to record price change timestamps

current_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

#Print price and timestamp information

print(f"Current price: ${price_float}")

print(f"Timestamp: {current_date}")

Be sure to replace the value of the amazon_url variable with the Amazon product page URL you copied. Additionally, depending on the actual page structure, you may need to adjust the selector to target the price element. In this example, we assume that the price element is contained in a span tag with the class name a-offscreen. If the actual situation is different, modify the selector accordingly.

3. Run scripts and test tracking functions

Save and run the script. If everything is fine, you will see the item's price and current timestamp printed on the console. You can run the script multiple times to check if the price has changed. If the price changes, you can record the timestamp and the new price value to compare with previous records.

Please note that Amazon may take action against frequent crawling of its site, so make sure your crawling practices comply with Amazon's terms of use and policies. Additionally, since the structure of your site may change at any time, you may need to regularly check and adjust your code to accommodate page updates.


In this article:
logo
PIA Customer Service
logo
logo
👋Hi there!
We’re here to answer your questiona about PIA S5 Proxy.
logo

How long can I use the proxy?

logo

How to use the proxy ip I used before?

logo

How long does it take to receive the proxy balance or get my new account activated after the payment?

logo

Can I only buy proxies from a specific country?

logo

Can colleagues from my company use the same account as me?

Help Center

logo