Amazon price tracker using google app script - get email at specific date and time

CodeGuy
March 17, 2016

Amazon offers APIs for its affiliates, but as a non affiliate one can only think of web scraping as only means to fetch the product details. Or it could also the case where they don't offer every  detail through API. Most of the product comparison sites offer users to register for their price tracking service.
Here you will learn how to scrape using Google Apps Script which make use of (YQL) Yahoo Query Language . Below snippet 3 important factors need to be modified are,
  1. product link URL
  2. the XPATH
  3. the email address

To know the XPATH use the Chrome or Firefox Dev Tools to inspect the element, right click the node in the DOM tree and choose Copy XPath to know the XPath.

The next factor is when you have to be alerted about the price? whether Minutes, Hourly, Daily, Weekly, Monthly or at specific time. From your app script project set your Current Project's Triggers. Mean while change the email address in line 22



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function doGet() {

  // link of the webpage
  var url   = "http://www.amazon.com/Apple-Watch-Sport-Aluminum-42mm/dp/B00WUKULAC/ref=sr_1_1?s=electronics&ie=UTF8&qid=1458193705&sr=1-1&keywords=apple+watch";

  // price_xpath here
  var price_xpath = '//*[@id="priceblock_ourprice"]';

  // YQL URL (https://developer.yahoo.com/blogs/ydn/yql-query-builder-explorer-14031.html)

  var query = "select * from html where url = '" + url + "' and xpath = '" + price_xpath + "'";

  // Get JSON format
  var myyql   = "https://query.yahooapis.com/v1/public/yql?format=json&q=" + encodeURIComponent(query);

  // Parse the JSON response from YQL
  var json = JSON.parse(UrlFetchApp.fetch(myyql).getContentText());

  var result= "<b>Todays Price is: </b>" + json.query.results.span.content;

  //send mail
  GmailApp.sendEmail("youremail@gmail.com", "Digital Thoughts - Today's Price",  result, {htmlBody: result});

}

That it, you will receive alerts to you mail as per the triggers set. You can also modify the script for multiple items and mail you as per your need.

0 comments

Do not spam.

Like us on Facebook

Blog Archive