-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwater-quality.js
59 lines (54 loc) · 1.51 KB
/
water-quality.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
var request = require('request');
var cheerio = require('cheerio');
var async = require('async');
var fs = require('fs');
var _ = require('lodash');
var startingYear = process.argv[2] ? process.argv[2] : 2009;
var currentYear = process.argv[3] ? process.argv[3] : 2015;
var apiUrl = 'http://www.gzepb.gov.cn:81/was5/web/WaterQuality/MonthlyData.jsp';
var dataSet = {
updateDate: new Date(),
};
var queryTime = [];
for (var i = startingYear; i <= currentYear; i++) {
for (var j = 1; j <= 12; j++) {
queryTime.push(i + ',' + j);
}
}
async.map(queryTime, function(time, callback) {
request.post({
url: apiUrl,
form: {
sw: time
}
}, function(err, res, body) {
if (!err && res.statusCode == 200) {
var $ = cheerio.load(body);
var tempSet = [];
var monthlyData = [];
$('.report1_3').each(function(index, elem) {
tempSet[index] = $(this).text();
});
for (var k = 0; k < tempSet.length; k += 8) {
var item = {
'日期': tempSet[k],
'城市': tempSet[k + 1],
'水源地名称': tempSet[k + 2],
'本月取水量': tempSet[k + 3],
'水质类别': tempSet[k + 4],
'水质指数': tempSet[k + 5],
'水质状况': tempSet[k + 6],
'主要污染项目': tempSet[k + 7]
};
monthlyData.push(item);
}
callback(null, monthlyData);
}
});
}, function(err, result) {
if (err) { console.error(err); }
dataSet.result = _.flatten(result);
fs.writeFile('data.json', JSON.stringify(dataSet, null, 2), function(err) {
if (err) { console.error(err); }
});
});