访问JS全局变量的值在HTML与PhantomJS

问题描述 投票:1回答:2

所以,我有一段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();
javascript jquery node.js web-scraping phantomjs
2个回答
0
投票

看来你就应该能够使用

var foo = page.evaluate(function() {
  return window.foo
})
console.log('foo =', foo)

0
投票

这里是你将如何,从虚拟的幻影浏览器中打开的网址,并得到一个javascript返回值从网页幻像

const phantom = require('phantom');


openUrl = (req, res, next) => {
    let url = 'your url goes here';
    const {content, returnedFromPage} = await loadJsSite(url);
    return res.json(returnedFromPage);
}

loadJsSite = async (url) => {
  return new Promise( async (resolve, reject) => {

    const instance = await phantom.create();
    const page = await instance.createPage();
    await page.on('onResourceRequested', function(requestData) {
      console.info('Requesting', requestData.url);
    });

    const status = await page.open(url);
    var returnedFromPage = await page.evaluate(function() {
            return document.title;
        });
    const content = await page.property('content');

    await instance.exit();

    return resolve({content: content, returnedFromPage: returnedFromPage});

  })
}
© www.soinside.com 2019 - 2024. All rights reserved.