-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
94 lines (45 loc) · 1.61 KB
/
app.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var request = require('request');
var cheerio = require('cheerio');
var path = require('path');
var fs = require('fs');
var requrl = 'http://1024.97luhi.info/pw/htm_data/3/1705/635356.html';
console.log('---------------开始抓取页面内容---------------');
request(requrl,function(error, response, body) {
if (!error && response.statusCode == 200) {
acquireData(body);
}
})
function acquireData(data) {
var $ = cheerio.load(data);
var meizi = $('#read_tpc img').toArray();
var len = meizi.length;
console.log(len);
for (var i = 0; i < len; i++) {
var pics_src = meizi[i].attribs.src;
console.log(pics_src);
imgsrc = pics_src + '';
var filename = parseUrlForFileName(imgsrc); //生成文件名
downloadImg(imgsrc, filename,
function() {
});
}
}
function parseUrlForFileName(address) {
var filename = path.basename(address);
return filename;
console.log('file');
}
var downloadImg = function(uri, filename, callback) {
request.head(uri,function(err, res, body) {
console.log('res:' + res);
//
console.log('content-type:', res.headers['content-type']); //这里返回图片的类型
//
console.log('content-length:', res.headers['content-length']); //图片大小
if (err) {
console.log('err:' + err);
return false;
}
request(uri).pipe(fs.createWriteStream('images/' + filename)).on('close', callback); //调用request的管道来下载到images文件夹下
});
};