Set a string as the response of a webpage in phant

2019-03-01 16:56发布

问题:

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. Can that be done?

回答1:

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.



标签: phantomjs