想要自動從網頁抓一些數據或者想把一坨從什麼博客上拉來的數據轉成一種有結構的數據?javascript
1
|
npm install cheerio
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
var
http = require(
"http"
);
// Utility function that downloads a URL and invokes
// callback with the data.
function
download(url, callback) {
http.get(url,
function
(res) {
var
data =
""
;
res.on(
'data'
,
function
(chunk) {
data += chunk;
});
res.on(
"end"
,
function
() {
callback(data);
});
}).on(
"error"
,
function
() {
callback(
null
);
});
}
|
1
|
node download.js
|
1
2
3
4
5
6
7
8
|
download(url,
function
(data) {
if
(data) {
console.log(data);
}
else
console.log(
"error"
);
});
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
var
cheerio = require(
"cheerio"
);
download(url,
function
(data) {
if
(data) {
//console.log(data);
var
$ = cheerio.load(data);
$(
"div.artSplitter > img.blkBorder"
).
each
(
function
(i, e) {
console.log($(e).attr(
"src"
));
});
console.log(
"done"
);
}
else
console.log(
"error"
);
});
|
1
|
$(
"div.artSplitter > img.blkBorder"
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
var
cheerio = require(
"cheerio"
);
download(url,
function
(data) {
if
(data) {
// console.log(data);
var
$ = cheerio.load(data);
$(
"article"
).
each
(
function
(i, e) {
var
link = $(e).find(
"h2>a"
);
var
poster = $(e).find(
"username"
).text();
console.log(poster+
": ["
+link.html()+
"]("
+link.attr(
"href"
)+
")"
);
});
}
});
|
1
|
$(
"article"
)
|
1
|
var
link = $(e).find(
"h2>a"
);
|
1
|
var
poster = $(e).find(
"username"
).text();
|
Jaime Edmondson heats up football fashion with scorching pictorial
transformice The Latest in Window Treatment Stylescss