CasperJS评估函数不返回数组 [英] CasperJS Evaluate function not returning Array

查看:60
本文介绍了CasperJS评估函数不返回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意,我已经看过:

了解CasperJS中的评估功能

我正在尝试编写一个简单的网络抓取工具,以将所有pdf下载到我的网站上教授的网页。

I am trying to write a simple web-scraper to download all pdf's on my professor's webpage.

这是我的代码:

var casper = require('casper').create({verbose: true , logLevel: "debug" });
var url = "https://www.cs.rit.edu/~ib/Classes/CSCI264_Fall16-17/assignments.html";
casper.start(url);

var elements; 
try {
    casper.then(function(){
        try {
        // statements
        elements = this.evaluate(function(){ return __utils__.findAll('body ul li a'); });
        console.log("elements: " + elements);
        console.log(this.getCurrentUrl());

        } catch(e) {
            // statements
            console.log(e);
        }   
    });
} catch(e) {

    console.log(e);
}
casper.run();

返回的元素数组大小始终是零,但当我输入

The elements array size given back is always zero but when I put

__ utils __。echo(__ utils __。findAll('body ul li a')。length);

我得到了正确数量的链接。

I get the correct amount of links.

这是因为评估函数不会返回元素数组吗?

Is this because the evaluate function won't return an array of elements?

任何帮助将不胜感激。

Any help would be appreciated.

推荐答案

只需使用本机js方法,而不是casperjs提供的 __ utils __ ,示例:

Just use native js methods instead of __utils__ provided by casperjs, example:

elements = this.evaluate(function(){ return document.querySelectorAll('body ul li a'); });

我不确定为什么 findAll 没有没事。

I'm not sure why findAll didn't work.

这篇关于CasperJS评估函数不返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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