-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb.js
More file actions
executable file
·48 lines (38 loc) · 1.21 KB
/
web.js
File metadata and controls
executable file
·48 lines (38 loc) · 1.21 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
#!/usr/bin/env node
var http = require('http');
var events = require('events');
var inherits = require('util').inherits;
var nquery = require('../lib/nquery');
function WebClient(host, path) {
var self = this,
transport = http.createClient(80, host),
request = transport.request('GET', path, {'host': host});
request.end();
request.on('response', function (response) {
if (response.statusCode != 200) {
self.emit('done', response.statusCode, '');
}
else {
var html = '';
response.setEncoding('utf8');
response.on('data', function (chunk) {
html += chunk;
});
response.on('end', function (chunk) {
self.emit('done', 200, html);
});
}
});
}
inherits(WebClient, events.EventEmitter);
var client = new WebClient('www.yahoo.com', '/');
client.on('done', function(status, html) {
if (status != 200) {
throw 'unable to download page';
}
var $ = nquery.createHtmlDocument(html);
var $links = $('div').find('a');
for (var i = 0; i < $links.length; i++) {
console.log($links[i].toString());
}
});