Set a string as the response of a webpage in phantomjs

前端 未结 1 626
广开言路
广开言路 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("Test#1

    Test #1

    Something

    ","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)
提交回复
热议问题