首页 > 编程 > JavaScript > 正文

nodejs实现获取某宝商品分类

2019-11-20 12:24:05
字体:
来源:转载
供稿:网友

闲来无事,研究下电商网站,正好朋友在开某宝店,看到琳琅满目的商品分类,见猎心喜。于是稍微研究了一下。

商品分类获取全由AJAX完成,且因为需要登录,我闲麻烦,就采取了最简单的办法,进入后台直接打开控制台,把代码粘进去运行,嘿嘿,所有分类都跑到CAT.data中去了。

菜鸟练手,求指点。

var CAT = {  //[{id: '', name: '', data: [{id: '', name: '', data:[{id: '', name: ''}]},{}, ...]}, {} ...]  data: [],  url: function(){    return 'http://upload.taobao.com/auction/json/reload_cats.htm?t='+Math.random();  },  init: function(){    var url = CAT.url(),      post_data = 'path=all';    CAT.ajax(url, post_data, CAT.first_r);  },  first_r: function(data){    var rs = data[0]['data'],      first_l, first_d, i, j, second_id, second_d, func;    for(i=0;i<rs.length; i++){      //保存一级分类      first_d = rs[i]['data'];      first_l = [];      for(j=0; j<first_d.length; j++){        //保存二级分类同时查询三级分类,并提供存储数据的容器        second_id = first_d[j]['sid'];        second_d = {          'id': first_d[j]['sid'],          'name': first_d[j]['name'],          'spell': first_d[j]['spell'],          'data': []        };        first_l.push(second_d);        func = CAT.second_r(second_d['data']);        CAT.ajax(CAT.url(), 'path=next&sid='+second_id, func);      }      CAT.data.push({        'id': rs[i]['id'],        'name': rs[i]['name'],        'data': first_l      })    }  },  second_r: function(container){    return function(data){      if(data.length<1){        return      }      var rs = data[0]['data'],        i, j, here, third_d;      for(i=0; i<rs.length; i++){        third_d = rs[i]['data'];        for(j=0; j<third_d.length; j++){          here = third_d[j];          container.push({            'id': here['sid'],            'name': here['name'],            'spell': here['spell']          });        }      }    }  },  ajax: function(url, post_data, func){    var xhr = new XMLHttpRequest(),    result;    xhr.open('POST', url, true);    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");    xhr.send(post_data);    xhr.onreadystatechange=function(){      if (xhr.readyState==4 && xhr.status==200){        result = JSON.parse(xhr.responseText);        func(result);      }else if(xhr.readyState==4 && (!xhr.status==200)){        console.log('Ajax Return Error!');      }    }  }};CAT.init();

以上所述就是本文的全部内容了,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表