2013-11-04 25 views
0

我正在解析來自Alexa類別的數據,如one解析來自Alexa的排名

我的代碼可以找到here

我正在獲取網站的URL列表,然後嘗試獲取每個網站的排名。問題是我回來了一個很好的網站列表(數組)和一個混亂的排名列表(也是一個數組,但超出了預期的順序)。到目前爲止,我的線索是有關節點阻塞功能的問題(我使用async.series一次運行一個函數,但它不起作用),for循環肯定有問題。

我使用「節點」從命令行運行此命令。任何建議/幫助非常感謝!謝謝。

回答

0

兩個問題與您的代碼:爲i

  • 排名插入的範圍不incremently發生

    1. 關閉問題(我i是這樣4 0 2 1 3 5 7 6 12 11 9 13 8 15 16 18 21 19 17 22 23 20 14 24 10

    要解決這種變化你的第二個功能是這樣的:

    ... 
    function (callback) { 
        for (i = 0; i < websites.length; i++) { 
         clr(i); 
        }; 
    }], function (error) { 
        if (error) { 
         console.log(error); 
        } 
    }); 
    
    function clr(i){ 
        alexaurl = "http://www.alexa.com/siteinfo/" + websites[i]; 
        request(alexaurl, function (error, response, body) { 
         $ = cheerio.load(body); 
         $('.siteInfoPage #traffic-rank-content .metricsUrl a').each(function() { 
          //ranks.push($(this).text()); 
          ranks[i]=$(this).text(); 
          return false; // so it does not count the a:hover element 
         }); 
         console.log(i); 
         console.log(websites); 
         console.log(ranks); 
         console.log("The website " + websites[i] + " has a rank of " + ranks[i]); 
        }); 
    } 
    
  • +0

    真的很感謝你的幫助。 –