-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
52 lines (49 loc) · 1.38 KB
/
app.js
File metadata and controls
52 lines (49 loc) · 1.38 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
46
47
48
49
50
51
52
var request = require('request'),
fs = require('fs'),
cheerio = require('cheerio'),
data = [];
request('http://library.itats.ac.id/index.php?search=search', function (error, response, html) {
if (!error && response.statusCode == 200) {
//if using form with method get
// request.get({
// url : 'http://library.itats.ac.id/index.php',
// qs: {
// 'search': 'search',
// 'keywords' : 'optimasi'
// },
// } , function(err, res, body) {
// run(body);
// });
run(html);
}
});
function run(e){
var $ = cheerio.load(e);
var length = $('.item').length;
for(var i =0;i < length;i++){
var judul = $($($('.item')[i]).find('a')[0]).html();
var pengarang = $($($('.item')[0]).find('div.author')[0]).text().split(':')[1];
data.push({
title: judul,
author: pengarang
});
}
if($($('.next_link')[0]).attr('href')){
console.info($($('.next_link')[0]).attr('href'));
next($($('.next_link')[0]).attr('href'));
}else{
fs.writeFile("data.txt", JSON.stringify(data,null,2), function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
}
}
function next(e){
request('http://library.itats.ac.id'+e, function (error, response, html) {
if (!error && response.statusCode == 200) {
run(html);
}
});
}