-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
89 lines (79 loc) · 2.36 KB
/
parser.js
File metadata and controls
89 lines (79 loc) · 2.36 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var request = require('request');
var cheerio = require('cheerio');
process.on('message', function (msgobj) {
console.log('Child got message:', msgobj.text);
console.log('Child pid: ' + process.pid);
var headers = {
'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'text/plain'
};
var options = {
url: 'http://gamebomb.ru/news',
method: 'GET',
headers: headers
};
var myData = [];
var getData = function (callback) {
request(options, function (error, response, body) {
if (error) {
return callback(error, null);
} else {
myData = [];
var $ = cheerio.load(body);
$('.sub:not(.gray)').each(function () {
var link = $(this);
var text = link.next().text();
var href = $(this).next().attr('href');
myData.push({
link: href,
title: text
});
});
return callback(null, myData);
}
});
};
var pageData = [];
getData(function (err, rez) {
if (err) {
console.log(err.message);
process.exit(0);
return;
}
getUrl();
});
function getUrl() {
if (myData.length === 0) {
process.send(pageData);
process.exit(0);
return;
}
console.log('Запрос: ' + myData.length);
var next = myData.shift();
makeRequest(next, getUrl);
}
function makeRequest(url, callback) {
request(url.link, function (err, response, body) {
if (err) {
callback();
}
console.log(url);
var textString = '';
var $ = cheerio.load(body);
$('.container-margin p').each(function () {
var link = $(this);
var text = link.text().replace(/^\s+|\s+$/g, '');
if (text !== '') {
console.log(text);
textString += text;
}
});
pageData.push({
link: url.link,
title: url.title,
text: textString
});
callback();
});
}
});