节点js,为某些网站请求正文

我正在试验Node.js和网页抓取。 在这种情况下,我试图从当地的广播电台上播放最近的歌曲来播放。 有了这个特定的网站, body没有任何回报 当我尝试使用谷歌或任何其他网站, body有一个价值。 这是我试图刮去的网站的function吗?

这是我的代码:

 var request = require('request'); var url = "http://www.radiomilwaukee.org"; request(url, function(err,resp,body) { if (!err && resp.statusCode == 200) { console.log(body); } else { console.log(err); } 

});

这很奇怪,你要求的网站似乎不会返回任何东西,除非accept-encoding头被设置为gzip 。 考虑到这一点,使用这个要点将工作: https : //gist.github.com/nickfishman/5515364

我在该要点内运行了代码,用"http://www.radiomilwaukee.org"代替URL,并在代码完成后查看sample.html文件中的内容。

如果你想在代码中访问网页的内容,你可以这样做:

 // ... req.on('response', function(res) { var body, encoding, unzipped; if (res.statusCode !== 200) throw new Error('Status not 200'); encoding = res.headers['content-encoding']; if (encoding == 'gzip') { unzipped = res.pipe(zlib.createGunzip()); unzipped.on("readable", function() { // collect the content in the body variable body += unzipped.read().toString(); }); } // ...