Created
May 16, 2013 16:00
-
-
Save wickchucked/5592836 to your computer and use it in GitHub Desktop.
Finds products from test page and reads title, price, image url, and a boolean for the in-stock status.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var cheerio = require('cheerio'); | |
// Requirements | |
// returns the title, price, image url, and a boolean for the in-stock status? | |
// | |
// IN STOCK PRODUCT | |
// http://parser-static.trackif.com/product.php | |
// | |
// <div class="hero-unit"> | |
// <img src="images/product_sample_image.jpg" class="pull-left" id="product-image" style="margin-right:20px"> | |
// <h1>Fantastic Santa Hat</h1> | |
// <h2>$29.99</h2> | |
// <p>This Santa hat is perfect for the holidays as well as year round!</p> | |
// <p><a href="#more-info" class="btn btn-primary btn-large">Learn more »</a></p> | |
// <div class="clearfix"></div> | |
// </div> | |
// | |
// OUT OF STOCK PRODUCT | |
// http://parser-static.trackif.com/product-out-of-stock.php | |
// | |
// <div class="hero-unit"> | |
// <img src="images/product_sample_image.jpg" class="pull-left" id="product-image" style="margin-right:20px"> | |
// <h1>Perfect Santa Hat</h1> | |
// <h2>$59.99</h2> | |
// <p>This Santa hat is the best one that exists.</p> | |
// <p>Status: <span class="stock-status" style="color:#f00">Out Of Stock</span></p> | |
// <div class="clearfix"></div> | |
// </div> | |
// var url = 'http://parser-static.trackif.com/product-out-of-stock.php'; | |
var url = 'http://parser-static.trackif.com/product.php'; | |
var imgUrl = 'http://parser-static.trackif.com/'; | |
request(url, function(error, response, body) { | |
if (!error && response.statusCode === 200) { | |
$ = cheerio.load(body); | |
var container = $('.hero-unit'), | |
title = $(container).find('h1').text(), | |
price = $(container).find('h2').text(), | |
imgLink = $(container).find('img').attr('src'), | |
status = $(container).find('.stock-status').length, | |
stockStatus; | |
// If .stock-status span is not found return true | |
(status === 0) ? stockStatus = true : stockStatus = false; | |
// Output the results | |
console.log('Title: ' + title); | |
console.log('Price: ' + price); | |
console.log('Image Url: ' + imgUrl + imgLink); | |
console.log('In Stock: ' + stockStatus); | |
} | |
else { | |
console.log('Bad request...') | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment