【问题标题】:Sharing variable scope with jsdom and jquery in Node在 Node 中与 jsdom 和 jquery 共享变量范围
【发布时间】:2014-11-05 01:10:56
【问题描述】:

所以,我正在用 jsdom 和 jquery 编写一个简单的页面抓取工具,但遇到了一个我不知道如何解决的问题。

这是一些有效的代码(更改了 URL):

var jsdom = require("jsdom");
var fs = require('fs');
var jquery = fs.readFileSync("./js/jquery-min.js").toString();

//There's two pages of product, here's page 1
jsdom.env({
        url: 'http://exampleshoppingpage.com',
        src: [ jquery ],
        done: function(error, window){
                var $ = window.$;
                $('.productlist .product .title a').each(function() {
                        console.log($(this).text());
                });
        } 
});

//And do the exact same thing for page 2
jsdom.env({
        url: 'http://exampleshoppingpage.com?page=2',
        src: [ jquery ],
        done: function(error, window){
                var $ = window.$;
                $('.productlist .product .title a').each(function() {
                        console.log($(this).text());
                });
        } 
});

但我真正想做的是获取所有这些产品并在打印出来之前对其进行分类。这是我尝试过的:

var jsdom = require("jsdom");
var fs = require('fs');
var jquery = fs.readFileSync("./js/jquery-min.js").toString();
var products = [];


//There's two pages of product, here's page 1
jsdom.env({
        url: 'http://exampleshoppingpage.com',
        src: [ jquery ],
        done: function(error, window){
                var $ = window.$;
                products $('.productlist .product .title a').each(function() {
                        products.push($(this).text());
                });
        } 
});

//And do the exact same thing for page 2
jsdom.env({
        url: 'http://exampleshoppingpage.com?page=2',
        src: [ jquery ],
        done: function(error, window){
                var $ = window.$;
                $('.productlist .product .title a').each(function() {
                        products.push($(this).text());
                });
        } 
});

products = products.sort();
console.log (products.join("\n"));

我得到一个空数组。我尝试了其他一些方法来确定我是否只是在做一些愚蠢的事情。我假设它与 jsdom 中的 jQuery 不与程序的外部共享范围有关?

【问题讨论】:

    标签: javascript jquery node.js jsdom


    【解决方案1】:

    在这种情况下,我们必须记住异步思考。您的范围很好,但您正试图在 products 填充数据之前将其转储到控制台。

    另外,Array.prototype.sort() operates on the array directly。它不返回数组。

    var jsdom = require("jsdom");
    var jquery = "http://code.jquery.com/jquery.js";
    
    var products = [];
    
    //  page 1
    jsdom.env({
            url: 'http://news.ycombinator.com/',
            scripts: [ jquery ],
            done: function(error, window){
                    var $ = window.$;
                    $('td.title:not(:last) a').each(function() {
                            products.push( $(this).text() );
                    });
                    //      page 2
                    jsdom.env({
                            url: 'https://news.ycombinator.com/news?p=2',
                            scripts: [ jquery ],
                            done: function(error, window){
                                    var $ = window.$;
                                    $('td.title:not(:last) a').each(function() {
                                            products.push( $(this).text() );
    
                                    });
                                    products.sort();
                                    console.log( products );
                            }
                    });
            }
    });
    

    【讨论】:

    • 这也是crazy and unreadable javascript 获取方式以及Promises will make life awesome 获取方式的一个很好的例子,恕我直言。
    • 啊!异步思维每次都能吸引我!尤其是当我在传统编程中来回切换时。
    • 我知道!我不能等到它只是所有的 javascript,一直
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 2018-09-07
    • 1970-01-01
    • 2021-08-07
    • 2014-09-05
    • 2021-02-17
    • 1970-01-01
    相关资源
    最近更新 更多