Flipkart Price API with Google Apps Script
Flipkart, the popular shopping website in India that sells everything from erasers to televisions, offers no API and therefore if you were to extract the pricing information of any Flipkart product, screen scraping is the only alternative.
Flipkart stores the pricing data inside <meta>
tags with “itemprop” set to “price” and it is thus relatively easy to pull this information for the price tracker tool.
Here’s the Google Apps Script code that extracts the price details, item title and the thumbnail image given the URL of the product page using Regular Expressions. You can easily use this in combination with HTMLService to create an API that returns pricing data for Flipkart product as JSON or XML.
function priceFlipkart(url) {
if (url !== '') {
try {
/* Extract the HTML source of the Flipkart Page */
var page = UrlFetchApp.fetch(url).getContentText();
/* Regular Expression to extract Price from the META tag */
var regex = /<meta[^>]*itemprop\s*=\s*"price"\s*content\s*=\s*"([^"]*)"/gi;
if ((price = regex.exec(page)) !== null) {
regex = /<meta[^>]*name\s*=\s*"og_title".*content\s*=\s*"([^"]*)/gi;
title = regex.exec(page);
/* We are using Canonical URL as it containes no tracking parameters */
regex = /<meta[^>]*name\s*=\s*"og_url".*content\s*=\s*"([^"]*)/gi;
canonical = regex.exec(page);
/* The thumbnail image of the Flipkart Product */
regex = /<meta[^>]*name\s*=\s*"og_image".*content\s*=\s*"([^"]*)/gi;
image = regex.exec(page);
if (title && canonical && image) {
Logger.log(title[1] + '|' + image[1] + '|' + price[1]);
} else {
Logger.log('Could not fetch ' + url);
}
}
} catch (e) {
Logger.log('Flipkart Error: ' + e.toString());
}
}
}
Amit Agarwal
Google Developer Expert, Google Cloud Champion
Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.
Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory