-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.js
79 lines (69 loc) · 2.32 KB
/
crawler.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
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
var _ = require('lodash');
var async = require('async');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var iconv = require('iconv-lite');
var schema = {
"单位名称": ["div.mima03 > table > tbody > tr:nth-child(1n + 2) > td:nth-child(2)"],
"等级": ["div.mima03 > table > tbody > tr:nth-child(1n + 2) > td:nth-child(3)"],
"医疗": ["div.mima03 > table > tbody > tr:nth-child(1n + 2) > td:nth-child(4)"],
"工伤": [
"div.mima03 > table > tbody > tr:nth-child(1n + 2) > td:nth-child(5)",
function(e) {
return !e.html().trim()?false:true;
}],
"生育": [
"div.mima03 > table > tbody > tr:nth-child(1n + 2) > td:nth-child(6)",
function(e) {
return !e.html().trim()?false:true;
}],
"地址": ["div.mima03 > table > tbody > tr:nth-child(1n + 2) > td:nth-child(7)"],
"行政区": ["div.mima03 > table > tbody > tr:nth-child(1n + 2) > td:nth-child(8)"]
};
var keys = _.keys(schema);
var url = 'http://www.gzyb.net/infoquery/QueryDdyljgData.action';
async.waterfall([
function(callback) {
request({
url: url,
formData: {
pageSize: 1000,
pageNo: 1
},
encoding: null
}, callback);
}, function(res, body, callback) {
if (res.statusCode != 200) return callback(new Error(res.statusCode));
var isGBK = res.headers['content-type'].toUpperCase().indexOf('GBK')>-1;
callback(null, isGBK?iconv.decode(body, 'GBK'):body.toString());
}, function(html, callback) {
var $ = cheerio.load(html);
var result = {};
_.forEach(schema, function(config, key) {
var selector = config[0];
result[key] = $(selector).map(function(i, el) {
var f = config[1];
if (f) return f($(el));
return $(el).text().trim();
}).get();
});
return callback(null, _.map(result[keys[0]], function(val, index) {
var row = {};
_.forEach(keys, function(key) {
row[key] = result[key][index];
});
return row;
}));
}], function(err, data) {
if (err) return console.dir(err);
var result = {
timeUpdated: Date.now(),
count: data.length,
result: data
};
fs.writeFile('data.json', JSON.stringify(result, null, 2), function(err) {
if (err) return console.dir(err);
console.log('success');
});
});