1:引用模塊"http" (執行命令node app.js "http://www.baidu.com")html
//app.js
var http = require('http');
var url = require('url');
function spider(u,cb){
http.get( url.parse(u), function(res){
var d = ''
res.on('data',function(chunk){
d += chunk;
})
res.on('end',function(){
console.log('spider_end && do cb');
cb(d);
})
});
};
var u = "";
if( require.main === module ) {
u = process.argv[2]
};
spider(u,function( data ){
//這個返回的是網頁內容的信息;
console.log( data );
});
2:引用模塊nodegrass: (執行命令node app.js "http://www.baidu.com")node
//app.js
var url = "http://www.cnblogs.com/xiaochao12345/archive/2014/10/23/4044950.html";
var ng = require('nodegrass');
if(require.main === module) {
console.log( process.argv );
url = process.argv[2];
}
ng.get(url,function(data){
//這個返回的是網頁內容的信息;
console.log(data); },'utf8');
3:引用模塊superagent: (執行命令node app.js "http://www.baidu.com")jquery
//app.js
var url = "http://www.cnblogs.com/xiaochao12345/archive/2014/10/23/4044950.html";
var superagent = require("superagent");
if(require.main === module) {
console.log( process.argv );
url = process.argv[2];
};
superagent.get(url)
.end(function (err, res) {
console.log('fetch successful');
console.log(res);
});
4:使用curl模塊:(執行命令node app.js "http://www.baidu.com")git
//app.js
var curl = require("curl");
var u = "";
if( require.main === module ) {
u = process.argv[2]
};
curl.get(u,function( data ){
console.log( arguments );
});
______________________________________________________________________________________________________________github
NODEJS中處理dom節點的方式(接口都和jq是統一的);app
1:引用cheeriodom
var cheerio = require("cheerio"); var html = "<html><body><div id=\"div1\">text</div></body></html>"; var $ = cheerio.load(html); console.log( "html" ); console.log( $.html() ); console.log( "#div1————〉html" ); console.log( $("#div1").html() ); console.log( "#div1----〉text" ); console.log( $("#div1").text() );
2:引用jquerycurl
var $ = require("jquery"); var $dom = $("<html><body><div id=\"div1\">text</div></body></html>"); console.log( $dom.find("#div1").text() );
3:引用jsdomide
var jsdom = require('jsdom');
var curl = require("curl");
var u = "https://github.com";
if( require.main === module ) {
u = process.argv[2];
};
curl.get(u,function( arg0 , html ){
//jsdom至關於打開了一個頁面,在這個頁面裏面運行js;
var document = jsdom.jsdom( html.body );
for(var a in html)
console.log(a);
var window = document.createWindow();
var script = document.createElement('script');
//script.src = 'http://code.jquery.com/jquery-1.4.2.js';
script.src = "http://127.0.0.1:81/js/jquery.min.js";
script.onload = function(){
console.log(1);
console.log( window.jQuery("body").text() );
};
document.head.appendChild( script );
});
_________________________________________________________________________________________________________________fetch
文件保存的話直接用nodeJS中的fs模塊:
var fs = require("fs");
fs.appendFile('file-name', "text_text_text_text" ,function(err){
if(err)throw err;
console.log('done')
});