-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnode.js
More file actions
61 lines (49 loc) · 1.7 KB
/
Copy pathcnode.js
File metadata and controls
61 lines (49 loc) · 1.7 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
53
54
55
56
57
58
59
60
61
// scape a specific info page from cnode/good.
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
var port = process.env.PORT || 3000;
var html = fs.readFileSync('index.html');
var noNewLine = function(char){
return char != '\n';
}
app.get('/', function(req, res){
url = "https://cnodejs.org/?tab=good"; // cnode the good parts
src = "https://cnodejs.org/";
request(url, function(err, response, html){
if(!err){
var $ = cheerio.load(html);
var title, link, author;
var cellLst = [];
var cap = 2;
$("span.put_good").each(function(i, element){
var cell = {title: "", link: "", author: ""};
var data = $(this).next();
// sometimes we may need integer instead of pure string, use `parseInt()`.
cell.title = data.text().replace(/\s\s+/g, '');
// obtain the value of an attribute by use `attr('ATTRNAME')`
cell.link = src + data.attr('href');
// or use .eq(i) to select element among children.
cell.author = $(this).parent().prev().prev().prev().children().first().attr('src');
console.log(cell);
cellLst.push(cell);
})
console.log(cellLst);
// fs.writeFile('output.json', JSON.stringify(json, null, 4), function(err){
// console.log("File successfully written!");
// })
var outputString = "";
for(var i=0; i<cellLst.length; i++){
outputString += JSON.stringify(cellLst[i]);
}
res.send("check your console!\n" + outputString);
// res.writeHead(200);
// res.write(html);
// res.end();
}
});
})
app.listen(port);
module.exports = app;