浪跡天涯 程式學習筆記心得

Node.js爬蟲範例

使用request.js, cheerio.js 分別發出請求及用jQuery語法解析回應:

var cheerio = require("cheerio"),
    request = require("request"),
    https = require("https"),
    url = "https://nodejs.org/api/https.html";


request(url,function(err,res,body){
    var $=cheerio.load(body); // 讓cheerio解析回應
    var titleText = $("#intro > a").text();
    console.log(titleText); //Node.js
});