-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.js
More file actions
45 lines (37 loc) · 1.27 KB
/
Copy pathexample.js
File metadata and controls
45 lines (37 loc) · 1.27 KB
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
42
43
44
45
var Scraper = require('./scraper');
/* Get the full url for the given page number */
function toListingUrl(pageNum) {
return 'http://www.drinksmixer.com/cat/1/'+pageNum;
}
/* Get full urls for all the pages in the given range */
function getListingUrls(startPage, endPage) {
var urls = [];
for(var i = startPage; i<endPage+1; i++) {
urls.push(toListingUrl(i));
}
return urls;
}
var listings = new Scraper();
var drinks = new Scraper();
listings.pageHandler = function(url, $) {
var urls = $('div.clr a').each(function() {
drinks.addUrl('http://www.drinksmixer.com/'+this.href);
});
};
drinks.pageHandler = function(url, $) {
var title = $('h1.recipe_title').text(),
instructions = $('div.instructions').text(),
ingredients = $('span.ingredient');
title = title.substring(0, title.length - 7);
ingredients = ingredients.map(function() {
var $this = $(this),
name = $this.find('.name').text(),
amount = $this.find('.amount').text();
return {name: name, amount: amount};
}).toArray();
console.log(title, instructions, ingredients);
};
console.log("Starting...");
listings.addUrls(getListingUrls(1,1));
listings.run(function() {console.log("Done with listings!");});
drinks.run(function() {console.log("Done with drinks!");});