Get javascript rendered html source using phantomjs

前端 未结 2 1734
无人共我
无人共我 2020-12-13 10:34

First of all, I am not looking for any help in development or testing environment. Also I am new to phantomjs and all I want is just the command line operation of phantomjs

相关标签:
2条回答
  • 2020-12-13 11:06
    var pagehtml = page.evaluate("function() {"+ 
      "return '<html><head>' + document.head.innerHTML + '</head>' + '<body>' + document.body.innerHTML + '</body></html>';" + 
    "}");
    
    
    fs.write('output.html',pagehtml,'w');
    
    0 讨论(0)
  • 2020-12-13 11:12

    Unfortunately, that is not possible using just the PhantomJS command line. You have to use a Javascript file to actually accomplish anything with PhantomJS.

    Here is a very simple version of the script you can use

    Code mostly copied from https://stackoverflow.com/a/12469284/4499924

    printSource.js

    var system = require('system');
    var page   = require('webpage').create();
    // system.args[0] is the filename, so system.args[1] is the first real argument
    var url    = system.args[1];
    // render the page, and run the callback function
    page.open(url, function () {
      // page.content is the source
      console.log(page.content);
      // need to call phantom.exit() to prevent from hanging
      phantom.exit();
    });
    

    To print the page source to standard out.

    phantomjs printSource.js http://todomvc.com/examples/emberjs/

    To save the page source in a file

    phantomjs printSource.js http://todomvc.com/examples/emberjs/ > ember.html

    0 讨论(0)
提交回复
热议问题