今、これ読んでます。
そしたらnode.jsでHTMLページを取得するサンプルがあったのでやってみた。
// 指定されたURLのHTMLを取得する関数
function download(url, savepath, callback){
var http = require('http');
var fs = require('fs');
var outfile = fs.createWriteStream(savepath);
var req = http.get(url, function(res){
res.pipe(outfile);
res.on('end', function(){
outfile.close();
callback();
});
});
}
// メイン(download関数の実行)
download("http://www.aozora.gr.jp/index_pages/person81.html",
"miyazawakenji.html",
function(){console.log("ok, kenji");
});
download("http://www.aozora.gr.jp/index_pages/person148.html",
"natsumesoseki.html",
function(){console.log("ok, soseki");
});
簡単に取得するだけだとこれでいいが、リダイレクトなどは全く動作に反映されない。# node download-node-func.jsちなみにHTMLを取得して出来たファイルの中身はこんな感じ。
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>302 Found</title> </head><body> <h1>Found</h1> <p>The document has moved <a href="http://mirror.aozora.gr.jp/index_pages/person81.html">here</a>.</p> </body></html>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>302 Found</title> </head><body> <h1>Found</h1> <p>The document has moved <a href="http://mirror.aozora.gr.jp/index_pages/person148.html">here</a>.</p> </body></html>このようにリダイレクト前のページが取得されている。

0 件のコメント:
コメントを投稿