Set a string as the response of a webpage in phantomjs

前端 未结 1 624
广开言路
广开言路 2021-01-23 12:04

Hi what i am trying to do is not to get the webpage as

page.open(url);

but to set a string that has already been retrieved as the page response

相关标签:
1条回答
  • 2021-01-23 12:47

    Yes, and it is as easy as assigning to page.content. It is usually also worth setting a page.url (as otherwise you might hit cross-domain issues if doing anything with Ajax, SSE, etc.), and the setContent function is helpful to do both those steps in one go. Here is the basic example:

    var page = require('webpage').create();
    page.setContent("<html><head><style>body{background:#fff;text:#000;}</style><title>Test#1</title></head><body><h1>Test #1</h1><p>Something</p></body></html>","http://localhost/imaginary/file1.html");
    
    console.log(page.plainText);
    page.render("test.png");
    phantom.exit();
    

    So call page.setContent with the "previously retrieved page response" that you have.

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