/
web.js
executable file
·73 lines (56 loc) · 1.68 KB
/
web.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
#!/usr/bin/env node
var http = require('http'),
fs = require('fs'),
events = require('events'),
inherits = require('sys').inherits,
dominiq = require('../lib/dominiq'),
sizzFact = require('../lib/sizzle');
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);
/*
function FileClient(path) {
var self = this;
fs.readFile(path, 'utf8', function (err, html) {
if (err) {
self.emit('done', err, '');
}
else {
self.emit('done', 200, html);
}
});
}
inherits(FileClient, events.EventEmitter);
*/
var client = new WebClient('www.yahoo.com', '/');
client.on('done', function(status, html) {
if (status != 200) {
throw 'unable to download page';
}
var doc = dominiq.createHTMLDocument(html),
window = {document:doc},
sizzle = sizzFact.sizzleInit(window),
divs = sizzle('div');
for (var i = 0; i < divs.length; i++) {
console.log(divs[i]);
}
});