访问JS全局变量的值在HTML与PhantomJS
所以,我有一段HTML的,看起来是这样的:
<html>
<body>
<script>
var foo = {
bar: []
};
</script>
</body>
</html>
我试图用PhantomJS提取foo.bar的价值。我会怎么做呢?到目前为止,我知道我会的结构是这样的:
var webPage = require('webpage');
var page = webPage.create();
page.open(MY_URL, function(status) {
var foo = page.evaluate(function(){
//gets javascript from the HTML in the response
// and extracts foo from there
});
});
console.log(someVar);
phantom.exit();
回答如下:
看来你就应该能够使用
var foo = page.evaluate(function() {
return window.foo
})
console.log('foo =', foo)