-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchsb.js
52 lines (49 loc) · 1.44 KB
/
watchsb.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
/**
* Created by WILL on 2016/8/18.
*/
var http = require('http');
var jsdom = require('jsdom');
var url = require('url');
var request = function (options, callback) {
var host = options.host || '';
var path = options.path || '';
var method = options.method | 'GET';
http.request({
host: host,
path: path,
method: method
}, function (res) {
var html = '';
res.on('data', function (data) {
html += data;
});
res.on('end', function () {
jsdom.env(html, [], function(error, window) {
if(!error) {
var elementsA = window.document.getElementsByTagName('a');
var hrefs = [];
for(var i in elementsA) {
var elementA = elementsA[i];
var href = elementA.getAttribute('href');
var uri = url.parse(href);
if(uri.protocol === 'http:') {
hrefs.push({
host: uri.host,
path: uri.path
});
}
}
callback(hrefs);
}
});
});
}).on('error', function (err) {
console.log(err);
}).end();
};
request({
host: 'www.cotlife.cn',
path: '/'
}, function(hrefs){
console.log(hrefs);
});