在phantomjs中将字符串设置为网页的响应 [英] Set a string as the response of a webpage in phantomjs
问题描述
我想做的是不要让网页显示为
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?
推荐答案
是的,就像分配给page.content一样容易.通常也值得设置page.url(否则,如果使用Ajax,SSE等进行任何操作,您可能会遇到跨域问题),并且setContent
函数有助于一次性完成这两个步骤.这是基本示例:
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();
因此,使用您拥有的先前检索到的页面响应"来调用page.setContent.
So call page.setContent with the "previously retrieved page response" that you have.
这篇关于在phantomjs中将字符串设置为网页的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!