var http = require('http'); var fs = require('fs'); var cheerio = require('cheerio'); var request = require('request'); var url = "http://www.air-level.com/"; function fetchPage(x) { //封裝了一層函數 request(x,function(error,response){ var $ = cheerio.load(response.body); //採用cheerio模塊解析html var time = $('.text-center h4').text().trim(); var img_src = $('.text-center img').attr("src").trim(); var filename=img_src.substr( img_src.lastIndexOf('/')) request(img_src).pipe(fs.createWriteStream('./image/' +filename)); }) } fetchPage(url); //主程序開始運行 setInterval(function(){ fetchPage(url); },1000*60*30) process.on('uncaughtException', function (err) { //打印出錯誤 console.log(err); //打印出錯誤的調用棧方便調試 console.log(err.stack); });
代碼很簡單,不解釋!從air-level.com上爬下來圖片,保存於本地文件夾,效果以下:javascript
如今我已經到廣州了,不多再去關注霧霾了,這幾日北方的霧霾也輕了些,希望能一直如此!html