DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript綜合知識 >> nodejs實現獲取某寶商品分類
nodejs實現獲取某寶商品分類
編輯:JavaScript綜合知識     

   這篇文章主要介紹了nodejs實現獲取某寶商品分類,十分的簡單實用,進入後台直接打開控制台,把代碼粘進去運行就OK了,有需要的小伙伴可以參考下。

  閒來無事,研究下電商網站,正好朋友在開某寶店,看到琳琅滿目的商品分類,見獵心喜。於是稍微研究了一下。

  商品分類獲取全由AJAX完成,且因為需要登錄,我閒麻煩,就采取了最簡單的辦法,進入後台直接打開控制台,把代碼粘進去運行,嘿嘿,所有分類都跑到CAT.data中去了。

  菜鳥練手,求指點。

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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();

  以上所述就是本文的全部內容了,希望大家能夠喜歡。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved