获取知乎的话题数据,js写的,但偶尔还是被知乎服务器拦截掉。。。
By
shiqiren
at 2015-08-11 • 1人收藏 • 3679人看过
var topicArr=new Array();
function GetZhihuData(childID,parentId){
$.post("http://www.zhihu.com/topic/"+parentId+"/organize/entire?child="+childID+"&parent="+parentId, function(data) {
var result=JSON.parse(data);
var childData=result['msg'];
for(var i=0;i<childData.length;i++){
GetData(childData[i],parentId);
}
});
}
function GetData(childData,parentId){
if(childData.length==0) return;
if(childData[0]==="topic"){
var id=childData[2];
var title=childData[1];
if(typeof topicArr[id] !='undefined') return;
topicArr[id]=true;
console.log("insert into carbon_dict (id,title,parentId) values('"+id+"','"+title+"','"+parentId+"')");
}
else if(childData[0]==="load"){
var nextChild=childData[2];
var nextParent=childData[3];
GetZhihuData(nextChild,nextParent);
}
else{
for(var i=0;i<childData.length;i++){
GetData(childData[i],parentId);
}
}
}
console.log('start');
GetZhihuData('','19776749');//跟话题
11 个回复 | 最后更新于 2015-08-13
登录后方可回帖
啊不对,你说的是偶尔被拦截,那应该是请求频率过高了,这样很容易被封IP的。


粤公网安备 44030602003677号
被拦截应该是JavaScript的跨域处理机制,这部分属于跨域脚本,除非知乎那边做特别设置,不然一般情况都是不可运行的。
我找了篇文章是介绍这个的:http://blog.jobbole.com/53487/