September 21, 2021

Python Tutorial 17: Get the Up-to-date Amazon Product Market Value by Amazon Price Tracker Using ASIN, gspread, Oauth2Client, and Google Sheet

Alert of up-to-date or even real-time product price information is indispensable if you want to keep your business and marketing in a winning position. Also, the automatic update can integrate with your eCommerce P&L calculator. That means you can see if you set the pricing too high or too low. Don’t panic when the conversion rate goes down. That might be none of your marketing strategies. Meanwhile, it might be just because of your competitor’s pricing update.

python tutorial

Google Sheets ImportXML in a way can do a similar task like Python product pricing tracker. However, every Google sheet only allows running 50 cells of importXML at the same time. Frankly, it’s not sufficient if you are fetching different dimensional data. Being said, it is much easier and less complex. If you are interested, please check out my article regarding Google Sheets ImportXML.

Web Scraping with Google Sheets ImportXML to Automatically Collect Product Price Info

In this Python Tutorial, I would walk you through how to use Python to create a more all-rounded Amazon product price tracker. This tracker can be extended to update the data automatically without any manual work. Before sharing the schedule script in the next Python Tutorial, you can learn how to visit product pages by using Asin, and find, fetch and upload the price data to Google Sheets.

Python Tutorial – Install gspread and Oauth2Client Module

Gspread is a super simple Google Sheets Python API. You can continue to use Google Sheets API as I shared previously, for reading and writing new sheets and data. This article would use gspread, and basically, the methodology is similar. But the methods and logic are more simple

First thing first, we need to install gspread in your Macbook terminal

Pip3 Install gspread

Then, we need to install the upgraded OauthClient module. This is for you to connect the Google Sheet API and activate the service account JSON file.

Pip3 Install --upgrade oauth2client

Third, we need to create a Google API project and service account. For more details, please check out the other article that I shared previously.

Defining the Google Sheet API scope is necessary. As we need to connect with Google drive as well, there are 3 scopes for creating an Amazon price tracker. Below I create a variable called scope.

scope = ['https://www.googleapis.com/auth/spreadsheets',

'https://www.googleapis.com/auth/drive.file',

'https://www.googleapis.com/auth/drive']

Then, you download the JSON file after having created a service account in Google API. We would create a variable called creds and add the JSON file name in the below coding.

creds = ServiceAccountCredentials.from_json_keyfile_name('ajljou4098erqwie08234.json',scope)

Last but not least, gspread is used to read, update the Google Sheet. So we can create a variable called client, and authorize creds to this variable.

client = gspread.authorize(creds)

Python Tutorial – Get and Read the scraped Amazon Product ASIN

In Python Tutorial 16, we’ve scraped a bulk of product information, that includes ASIN. So along with the amazon product scraper, we can continue to use the same Google sheet file.

First thing first, we need to open the Google sheet file, where we use the open method.

sh = client.open('AmazonPriceTracker')

Then, we need to select the Google sheet tab that stores the product asin by using the get_worksheet method. Here the value starts from 0. 0 represents the 1st tab and then is 1, 2, and so on.

worksheet = sh.get_worksheet(2)

Depending on which column or row we save the asin data, we can leverage the methods of either col_value or row_value. For more methods given by gspread, please check out the documentation

values_list = worksheet.col_values(2)

Visit the Product Page and Fetch the Price Data

Now, value_list has got the scraped product asin. The next step is to loop and visit the product page for fetching the pricing data. In the price element, there are some places showing the product pricing. It depends on the Amazon page update. In a way, there are 4 places, which are the list price, current price, buybox price, and new buybox price. Here would show the list price for the example.

python tutorial

Again, as mentioned in the previous Python Tutorial, Amazon bans the requests and BeautifulSoup directly visits the pages. So we need to leverage selenium. For more details, please check out the previous Python Tutorial.

Python Tutorial 16 – Amazon Product Scraper Using Selenium, BeautifulSoup and gspread

First thing first, we need to create a loop and use selenium, BeautifulSoup to gain the price HTML information.

for asin in values_list:
      competitor = driver.get(f'https://www.amazon.com/dp/{asin}')
      soup = BeautifulSoup(driver.page_source, 'html.parser')
      time.sleep(5)

Then, under the parent tree of the asin looping, we need to find the path of the pricing data and create a looping for it as well. As you can see, the list price element is located in the span and the class of priceBlockStrikePriceString a-text-strike. So let us write the codes as listed below

for price in soup:

     try:
         ListPrice = price.find('span','priceBlockStrikePriceString a-text-strike').text.replace('$','')
     except Exception as e:
         ListPrice = '0'

Just kindly remind me that in the try and except, it’s better to set the none into a number, such as 0. It facilitates you to find the lowest price later on in the price tracker.

Append and Reset the Fetched Data Uploaded to Google Sheet

First thing, we can create an empty value of variable amazonSERP. This is for appending the information later on.

As you can see, I assume we collect 4 price data and we can name them and group them in the variable of element_info. Then, in the Pandas module, we can append and frame this information in an organized format, by using append() and dataframe() methods.

element_info = {
"ListPrice": ListPrice,
"PagePrice": priceA,
"BuyboxPrice": priceBuybox,
"NewBuyboxPrice": NewpriceBuybox
}

amazonSERP.append(element_info)

df = pd.DataFrame(amazonSERP)

For updates to the Google sheets, gspread has a method of value_update, that includes 3 parts you can plug into value. They are the Sheet location, parameters, and body for the value to update.

As gspread requires an assigned format, we need to reset the current orders and formats by using reset_index() and values.tolist() method.

value_list2 = sh.values_update('ProductPagePricingTracker!D1', params={'valueInputOption': 'USER_ENTERED'},body=dict(values=df.T.reset_index().T.values.tolist()))

Up-to-date Product Market Value in P&L Calculator

From below the D to G column, it shows the scraped different pricing data in an Amazon product page. Of course, we don’t recommend you plug these data into your P&L calculator. You can use the min() method to select the lowest one. So you can refer to the lowest one comparing with yours.

python tutorial

In the P&L calculator, apart from the flat and variable cost elements, you can observe the pricing market value of your products. As pricing is one of the key elements affecting the conversation rate, you should keep eye on that. The up-to-date information helps you make a better decision on the selling strategies and the digital advertising bidding and budget allocation.

For the retail market value in terms of the pricing, the higher percentage number represents the more competitive pricing point in the competition space. You can set up conditional formatting, so you can spot immediately when the number is highlighted into a red color. That means something is happening. For example, your competitors might be doing promotions.

Full Python Script of Amazon Product Price Tracker

If you would like to have the full version of the Python Script of Amazon Product Price Tracker, please subscribe to our newsletter by adding the message Python Tutorial 17. We would send you the script immediately to your mailbox.

Contact us

I hope you enjoy reading Python Tutorial 17: Get the Up-to-date Amazon Product Market Value by Amazon Price Tracker Using ASIN, gspread, OauthClient, and Google Sheet. If you did, please support us by doing one of the things listed below, because it always helps out our channel.

  • Support my channel through PayPal (paypal.me/Easy2digital)
  • Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
  • Follow and like my page Easy2Digital Facebook page
  • Share the article to your social network with the hashtag #easy2digital
  • Buy products with Easy2Digital 10% OFF Discount code (Easy2DigitalNewBuyers2021)
  • You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software
  • Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)

1 thought on “Python Tutorial 17: Get the Up-to-date Amazon Product Market Value by Amazon Price Tracker Using ASIN, gspread, Oauth2Client, and Google Sheet

Leave a Reply

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