60

我正在使用 PhantomJS page.evaluate() 进行一些抓取。我的问题是我传递给 webkit 页面的代码是沙盒的,因此无法访问我的主幻像脚本的变量。这使得抓取代码很难通用。

page.open(url, function() {
  var foo = 42;

  page.evaluate(function() {
    // this code has no access to foo
    console.log(foo);
  });
}

我怎样才能将参数推送到页面中?

4

8 回答 8

81

我有那个确切的问题。它可以用一点技巧来完成,因为它page.evaluate也可以接受一个字符串。

有几种方法可以做到这一点,但我使用了一个名为 的包装器evaluate,它接受附加参数以传递给必须在 webkit 端评估的函数。你会像这样使用它:

page.open(url, function() {
  var foo = 42;

  evaluate(page, function(foo) {
    // this code has now has access to foo
    console.log(foo);
  }, foo);
});

这是evaluate()功能:

/*
 * This function wraps WebPage.evaluate, and offers the possibility to pass
 * parameters into the webpage function. The PhantomJS issue is here:
 * 
 *   http://code.google.com/p/phantomjs/issues/detail?id=132
 * 
 * This is from comment #43.
 */
function evaluate(page, func) {
    var args = [].slice.call(arguments, 2);
    var fn = "function() { return (" + func.toString() + ").apply(this, " + JSON.stringify(args) + ");}";
    return page.evaluate(fn);
}
于 2012-03-23T11:13:38.433 回答
73

更改已被推送,现在您可以将其用作

page.open(url, function() {
  var foo = 42;

  page.evaluate( function(foo) {
  // this code has now has access to foo
  console.log(foo);
  }, foo);
}

推送详情在这里:https ://github.com/ariya/phantomjs/commit/81794f9096

于 2012-06-28T01:58:27.083 回答
12

您可以将参数传递给函数作为 page.evaluate 的参数。

例子:

page.evaluate(function(arg1, arg2){
    console.log(arg1); //Will print "hi"
    console.log(arg2); //Will print "there"
}, "hi", "there");
于 2018-05-17T03:54:42.213 回答
3

有适用于 PhantomJS 0.9.2 和 0.2.0 的解决方案:

page.evaluate(
    function (aa, bb) { document.title = aa + "/" + bb;}, //the function
    function (result) {}, // a callback when it's done
    "aaa", //attr 1
    "bbb"); //attr 2
于 2015-02-20T12:47:36.627 回答
2

另一种可能性:通过 url 传递变量。例如,传递对象 x

// turn your object "x" into a JSON string
var x_json = JSON.stringify(x);

// add to existing url
// you might want to check for existing "?" and add with "&"
url += '?' + encodeURIComponent(x_json);

page.open(url, function(status){
    page.evaluate(function(){
        // retrieve your var from document URL - if added with "&" this needs to change
        var x_json = decodeURIComponent(window.location.search.substring(1));
        // evil or not - eval is handy here
        var x = eval('(' + x_json + ')');       
    )}
});
于 2012-05-22T03:45:52.307 回答
1

这对我有用:

page.evaluate("function() {document.body.innerHTML = '" + size + uid + "'}");

意味着将所有内容都作为字符串。反正后来它变成了一个字符串。检查库源。

于 2015-02-17T19:39:46.330 回答
1

虽然您可以将参数传递给evaluate(function, arg1, arg2, ...),但这通常有点麻烦。特别是在传入多个变量或更糟的函数时。

为了绕过这个障碍,可以使用injectJs(filename)代替。

page.open(url, function() {
    if ( webpage.injectJs('my_extra_functionality.js') ) {
        page.evaluate( function() {
            // this code has access to foo and also myFunction();
            console.log(foo);
            console.log(myFunction());
        });
    }
    else {
        console.log("Failed to inject JS");
    }
}

my_extra_functionality.js同一目录中的本地文件在哪里:

var foo = 42;
var myFunction = function(){
    return "Hello world!";
}
于 2017-07-05T07:56:54.807 回答
0

你不能只将参数绑定到函数吗?

page.evaluate.bind(args)(callbackFn)
于 2016-08-19T01:41:36.080 回答