用 Node JS 看糗百段子

开源中国上看到有人写过Node JS版本的看糗百,然而现在用不了了,话说本身用JQuery解析就有点问题,所以自己写了一个。

网络爬虫用的nodegrass,解析用的原生JS

原帖 http://www.oschina.net/code/snippet_191191_24956


var nodegrass = require("nodegrass")

nodegrass.get('http://www.qiushibaike.com', function(data,status,headers){
var count=0;
var timer=setInterval(function(){
var index0=data.indexOf('糗友:');
var index1=data.indexOf('<br/><br/>');
if(index0==-1||index1==-1){
clearInterval(timer);
}
else{
console.log(count++);
console.log();
var author=data.substring(index0+3,index1);
data=data.substring(index1+10,data.length);
var content=data.substring(0,data.indexOf('<img')).replace(/<br\/>/g,'');
console.log("作者:" + (format(author) || "匿名"));
console.log("内容:" + format(content) + "\n\n");
}
},2000)
},'utf8').on('error', function(e){
  console.log('Got error:'+e);
});


function format(content){
    return content.replace(/\n/g, "");
}

https://tonicdev.com/npm/jQuery ​ 在线NodeJS,亲测可用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值