這篇文章主要介紹了nodejs實現獲取某寶商品分類,十分的簡單實用,進入后臺直接打開控制臺,把代碼粘進去運行就OK了,有需要的小伙伴可以參考下。
閑來無事,研究下電商網站,正好朋友在開某寶店,看到琳瑯滿目的商品分類,見獵心喜。于是稍微研究了一下。
商品分類獲取全由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();
以上所述就是本文的全部內容了,希望大家能夠喜歡。
新聞熱點
疑難解答
圖片精選