【问题标题】:Get generated HTML after JS manipulates the DOM and pass request headersJS 操作 DOM 并传递请求头后获取生成的 HTML
【发布时间】:2015-02-20 04:59:13
【问题描述】:

我需要在 JS DOM 操作完成后获取页面生成的 HTML 源代码。为此,我使用了 Phantomas https://github.com/macbre/phantomas,但不幸的是,它没有提供传入请求标头的方法。

是否有允许传递请求标头然后获取生成的 HTML 源代码的库。

任何指针都会很有帮助

【问题讨论】:

    标签: javascript jquery html html-parsing phantomjs


    【解决方案1】:

    您可以使用casperjs

    settings 对象中的headers 传递给open() function 并使用getPageContent() 获取页面的HTML 源代码:

    var casper = require('casper').create();
    
    var headers = {
        'Accept-Language': 'en-US,en;q=0.8',
        'HEADER-XYZ': 'HEADER-XYZ-DATA'
    };
    
    casper.start().then(function () {
        this.open("http://casperjs.org", {
            method: 'get',
            headers: headers
        });
    });
    
    casper.then(function() {
        console.log(this.getPageContent());
    });
    
    casper.run(function() {
        this.exit();
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用"PhantomJS WebKit scriptable"

      指定customHeaders 并获取page.content

      var webPage = require('webpage');
      var page = webPage.create();
      
      page.customHeaders = {
        "X-Test": "foo",
        "DNT": "1"
      };
      
      page.open('http://phantomjs.org', function (status) {
        var content = page.content;
        console.log('Content: ' + content);
        phantom.exit();
      });
      

      将其保存到test.js 并运行:

      phantomjs test.js
      

      【讨论】:

      • 如何通过命令行将 url 传递为phantomjs test.js http://phantomjs.org
      • @user567797 你可以阅读命令行参数,here 就是一个例子。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 2012-05-17
      • 1970-01-01
      • 2013-03-08
      • 1970-01-01
      • 2015-02-19
      • 2016-03-21
      相关资源
      最近更新 更多