Pass arguments with page.evaluate

2019-01-03 16:09发布

I'm using PhantomJS page.evaluate() to do some scraping. My problem is that the code I pass to the webkit page is sandboxed, and so has no access to the variables of my main phantom script. This makes it hard make the scraping code generic.

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

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

How could I push arguments into the page?

8条回答
我想做一个坏孩纸
2楼-- · 2019-01-03 17:00

The change has been pushed and now you can use it as

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

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

The push details are here: https://github.com/ariya/phantomjs/commit/81794f9096

查看更多
成全新的幸福
3楼-- · 2019-01-03 17:06

Can't you just bind the args to the function??

page.evaluate.bind(args)(callbackFn)
查看更多
登录 后发表回答