-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample2.js
41 lines (31 loc) · 1.11 KB
/
example2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// example2.js - Example #2 - Goes to http://www.horseheadspickapart.com/content.php?id=46
// and returns a JSON array of vehicle results
var request = require('request');
var cheerio = require('cheerio');
var host = 'http://www.horseheadspickapart.com';
var page = 'content.php?id=46';
var options = {
method: 'get',
uri: host + '/' + page,
json: true
};
var results = [];
request(options, function (error, response, body) {
if (error) {
return console.error('Error: ' + console.dir(error));
}
var $ = cheerio.load(body);
$('#featured_content').each(function() {
var vehicle = $(this).find('h4').text(); // Get vehicle
var date_added = $(this).find('h4').next().text();
var image = $(this).prev().find('img').attr('src'); // Get image
results.push({
'vehicle': vehicle,
'date_added': date_added,
'image': host + '/' + image,
//'full_image': host + '/new_photos/' + image.match(/(IMG_\d\d\d\d\.JPG)/g)
})
});
console.dir(results);
//console.log(JSON.stringify(results));
});