【问题标题】:Parsing rankings from Alexa从 Alexa 解析排名
【发布时间】:2013-11-15 01:03:39
【问题描述】:

我正在分析来自 Alexa 类别的数据,例如 one

我的代码可以在here找到。

我正在获取网站的 URL 列表,然后尝试获取每个网站的排名。问题是我得到了一个很好的网站列表(数组)和一个混乱的排名列表(也是一个数组,但超出了预期的顺序)。到目前为止,我的线索是节点阻塞功能有问题(我使用 async.series 一次运行一个函数但它不起作用)并且 for 循环肯定有问题。

我使用“节点”从命令行运行它。非常感谢任何建议/帮助!谢谢。

【问题讨论】:

    标签: javascript node.js html-parsing


    【解决方案1】:

    您的代码有两个问题:

    1. i 范围的闭包问题
    2. 排名插入不会逐步发生(对我来说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]);
        });
    }
    

    【讨论】:

    • 非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多