Javascript PhantomJS 从字符串创建页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11744089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PhantomJS create page from string
提问by mike
Is it possible to create a page from a string?
是否可以从字符串创建页面?
example:
例子:
html = '<html><body>blah blah blah</body></html>'
page.open(html, function(status) {
// do something
});
I have already tried the above with no luck....
我已经尝试过上述方法但没有运气....
Also, I think it's worth mentioning that I'm using nodejs with phantomjs-node(https://github.com/sgentle/phantomjs-node)
另外,我认为值得一提的是,我将 nodejs 与 phantomjs-node(https://github.com/sgentle/phantomjs-node) 一起使用
Thanks!
谢谢!
采纳答案by laker
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.
查看 phantomjs API, page.open 需要一个 URL 作为第一个参数,而不是一个 HTML 字符串。这就是您尝试的方法不起作用的原因。
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.
但是,您可能能够实现从字符串创建页面的效果的一种方法是托管一个空的“骨架页面”,在某处带有 URL(可能是 localhost),然后将 Javascript(使用 includeJs)包含到空页。您包含在空白页面中的 Javascript 可用于document.write("<p>blah blah blah</p>")
向网页动态添加内容。
I've ever done this, but AFAIK this should work.
我曾经这样做过,但 AFAIK 这应该有效。
Sample skeleton page:
示例骨架页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head></head>
<body></body>
</html>
回答by Ariya Hidayat
It's very simple, take a look at the colorwheel.jsexample.
很简单,看一下colorwheel.js的例子。
var page = require('webpage').create();
page.content = '<html><body><p>Hello world</p></body></html>';
That's all! Then you can manipulate the page, e.g. render it as an image.
就这样!然后您可以操作页面,例如将其呈现为图像。
回答by rahulroy9202
To do this you need to set the page content to your string.
为此,您需要将页面内容设置为您的字符串。
phantom.create(function (ph) {
ph.createPage(function (page) {
page.set('viewportSize', {width:1440,height:900})
//like this
page.set('content', html);
page.render(path_to_pdf, function() {
//now pdf is written to disk.
ph.exit();
});
});
});
you need to use page.set()
to set the html content.
您需要使用page.set()
来设置 html 内容。
as per https://github.com/sgentle/phantomjs-node#functionality-details
根据https://github.com/sgentle/phantomjs-node#functionality-details
Properties can't be get/set directly.
Instead use page.get('version', callback)or page.set('viewportSize', {width:640,height:480}), etc.Nested objects can be accessed by including dots in keys, such as page.set('settings.loadImages', false)
不能直接获取/设置属性。
而是使用page.get('version', callback)或 page.set('viewportSize', {width:640,height:480}) 等。可以通过在键中包含点来访问嵌套对象,例如 page.set('settings.loadImages', false)
回答by Klortho
I got the following to work in PhantomJS version 2.0.0. Whereas before, I was using page.open() to open a page from the filesystem and set a callback:
我在 PhantomJS 2.0.0 版中得到了以下内容。而之前,我使用 page.open() 从文件系统打开一个页面并设置一个回调:
page.open("bench.html", pageLoadCallback);
Now, I accomplish the same thing from a string variable with the HTML page. The page.setContent()
method requires a URL as the second argument, and this uses fs.absolute()
to construct a file://URL.
现在,我从 HTML 页面的字符串变量中完成了同样的事情。该page.setContent()
方法需要一个 URL 作为第二个参数,这用于fs.absolute()
构造一个file://URL。
page.onLoadFinished = pageLoadCallback;
page.setContent(bench_str, "file://" + fs.absolute(".") + "/bench.html");
回答by MGM
Just wanted to mention I recently had a similar need and discovered that I could pass file:// style references as an URL param, so I dumped my HTML string into a local file then passed the full path to my capture script (django_screamshot) which basically uses casperjs and phantomjs + a capture.js script.
只是想提一下,我最近有类似的需求,发现我可以将 file:// 样式引用作为 URL 参数传递,所以我将 HTML 字符串转储到本地文件中,然后将完整路径传递给我的捕获脚本 (django_screamshot)基本上使用 casperjs 和 phantomjs + 一个 capture.js 脚本。
Anyway it just works and its reasonably fast..
无论如何它只是有效并且相当快..