用CasperJS迭代网格 [英] Iterating over a grid with CasperJS

查看:140
本文介绍了用CasperJS迭代网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  |名称|我的意思是,我试图测试CasperJS,并且正在刮一个网格布局,如:名称| 
|标题|标题|
|图像|图像|
| Something | Something |
| ----------------------
|名称|名称|
|标题|标题|
|图像|图像|
| Something | Something |
| ----------------------

如果我没有使用CasperJS,我将检索所有包含的列表(我在这种情况下),然后在每个容器上运行一个方法,该方法可以检索具有所需属性的对象。



我似乎在CasperJS中很难做到这一点。首先,我尝试返回在casper.evaluate(function(){....})中的DOM元素列表,但它不能返回DOM元素。



然后,我试图做一个每个循环,将所需的对象(4)推送到一个数组,并将其返回到Evalue中,但它仍然返回null。



在CasperJS中做一些这样的事情。我可以以某种方式将一个容器的上下文返回到一个方法,它可以将对象返回到主要的评估,这可以返回对象的集合?

解决方案

不幸的是,你不能从 evaluate()函数获取一个复杂的结构,因为从是一种 JSON.parse(JSON.stringify(arg))



但这并不意味着你不能传递另一种对象。



这里有一个关于如何从 casper.evaluate()

  var arrayResult = this。评估(function getGridResuls(){

//创建数组
var arrayObjects = new Array();

//迭代表(grid)元素
jQuery(table.results)。each(function(index){

// get table(grid)
var tableResult = jQuery(th是);

//创建基本对象
objResult = new Object();

//填充对象属性
objResult.name = tableResult.find('selector to get name')。
objResult.title = tableResult.find('selector to get title')。text();
objResult.image = tableResult.find('selector to get image info')。text();
objResult.something = tableResult.find('selectot to get something')。text()。trim();

//将对象赋给数组
arrayObjects [index] = objResult;

});

//返回数组与对象
return arrayObjects;

});

...
//使用arrayResult

我假设网络上下文包含JQuery库。



提示:尝试运行 evaluate()函数,以确保您的js代码正常工作。


I am trying to test CasperJS out, and are scraping a site which has a grid layout like:

|Name      |Name      |
|Title     |Title     |
|Image     |Image     |
|Something |Something |
|----------------------
|Name      |Name      |
|Title     |Title     |
|Image     |Image     |
|Something |Something |
|----------------------

If I wasn't using CasperJS I would retrieve a list of all the contains (4 i this case) and then run a method on each container which could retrieve an object with the wanted properties.

I just seem to have a hard time of doing this in CasperJS. First I tried to return the list of DOM elements in casper.evaluate(function(){....}), but it can't return DOM elements.

Then I tried to make an each loop which would push the wanted objects (4) to an array and return it in an Evalue, but it keeps returning null.

How would one go about doing something like this in CasperJS. Can I somehow return a context of a container to a method, which can return the object to the main evaluate, which can the return the collection of the objects?

解决方案

Unfortunately, you can't get a complex structure from evaluate() function, because whatever arg passed from evaluate() is sort of JSON.parse(JSON.stringify(arg)).

But it doesn't mean that you are not able to pass another kind of objects.

Here an example about how get an array with objects from casper.evaluate():

var arrayResult = this.evaluate(function getGridResuls(){

    //create array
    var arrayObjects = new Array();

    //Iterates over table (grid) elements
    jQuery("table.results").each(function( index ) {

        //get table (grid)
        var tableResult = jQuery(this);

        //create basic object    
        objResult = new Object();

        //fill object properties
        objResult.name      = tableResult.find('selector to get name').text();
        objResult.title     = tableResult.find('selector to get title').text();
        objResult.image     = tableResult.find('selector to get image info').text();
        objResult.something = tableResult.find('selectot to get something').text().trim();

        //assign object to array
        arrayObjects[index] = objResult;

    });  

    //return array with objects
    return arrayObjects;

});

...
//do something with arrayResult

I'm assuming that the web context includes the JQuery library.

Tip: try to run the js code of the evaluate() function by using the browser console in order to be sure that your js code is working as expected.

这篇关于用CasperJS迭代网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆