标签:
想要自动从网页抓一些数据或者想把一坨从什么博客上拉来的数据转成一种有结构的数据?
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 Styles
标签:
原文地址:http://www.cnblogs.com/dowork/p/4847618.html