PhantomJS从字符串创建页面

PhantomJS从字符串创建页面

本文介绍了PhantomJS从字符串创建页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从字符串创建页面?

Is it possible to create a page from a string?

例如:

html = '<html><body>blah blah blah</body></html>'

page.open(html, function(status) {
  // do something
});

我已经尝试了以上但没有运气....

I have already tried the above with no luck....

此外,我认为值得一提的是我正在使用带有phantomjs-node的nodejs(https://github.com/sgentle/phantomjs-node)

Also, I think it's worth mentioning that I'm using nodejs with phantomjs-node(https://github.com/sgentle/phantomjs-node)

谢谢!

推荐答案

查看phantomjs ,page.open需要一个URL作为第一个参数,而不是HTML字符串。这就是你尝试的东西不起作用的原因。

Looking at the phantomjs API, page.open requires a URL as the first argument, not an HTML string. This is why the what you tried does not work.

但是,您可以通过字符串创建页面效果的一种方法是在某个地方托管一个空的骨架页面(可以是localhost),然后将Javascript(使用includeJs)包含在空白页面中。您包含在空白页面中的Javascript可以使用 document.write(< p> blah blah blah< / p>)来动态地向网页添加内容。

However, one way that you might be able to achieve the effect of creating a page from a string is to host an empty "skeleton page," somewhere with a URL (could be localhost), and then include Javascript (using includeJs) into the empty page. The Javascript that you include into the blank page can use document.write("<p>blah blah blah</p>") to dynamically add content to the webpage.

我做过这个,但是AFAIK应该可行。

I've ever done this, but AFAIK this should work.

示例骨架页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head></head>
<body></body>
</html>

这篇关于PhantomJS从字符串创建页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:24