PhantomJS如何在html字符串中呈现javascript [英] PhantomJS how to render javascript in html string

查看:83
本文介绍了PhantomJS如何在html字符串中呈现javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让PhantomJS获取一个html字符串,然后让它像浏览器那样呈现整个页面(包括在页面源中执行任何javascript)。我需要将生成的html结果作为字符串。我见过page.open的例子,因为我的数据库中已有页面源,所以没用。

I'm trying to get PhantomJS to take an html string and then have it render the full page as a browser would (including execution of any javascript in the page source). I need the resulting html result as a string. I have seen examples of page.open which is of no use since I already have the page source in my database.

我是否需要使用page.open来触发PhantomJS中的javascript渲染引擎?无论如何都要在内存中执行此操作(即..没有page.open发出请求或从磁盘读取/写入html源?

Do I need to use page.open to trigger the javascript rendering engine in PhantomJS? Is there anyway to do this all in memory (ie.. without page.open making a request or reading/writing html source from/to disk?

我看过一个类似的问题和答案这里,但它并没有完全解决我的问题。运行后下面的代码,我没做什么似乎在html源代码字符串中呈现javascript。

I have seen a similar question and answer here but it doesn't quite solve my issue. After running the code below, nothing I do seems to render the javascript in the html source string.

var page = require('webpage').create();
page.setContent('raw html and javascript in this string', 'http://whatever.com');
//everything i've tried from here on doesn't execute the javascript in the string

--------------更新---------------

--------------Update---------------

根据以下建议尝试以下操作,但这仍然不起作用。只需返回原始我提供的源代码没有呈现javascript。

Tried the following based on the suggestion below but this still does not work. Just returns the raw source that I supplied with no javascript rendered.

var page = require('webpage').create();
page.settings.localToRemoteUrlAccessEnabled = true;
page.settings.webSecurityEnabled = false;
page.onLoadFinished = function(){
    var resultingHtml = page.evaluate(function() {
        return document.documentElement.innerHTML;
    });
    console.log(resultingHtml);
    //console.log(page.content); // this didn't work either
    phantom.exit();
};
page.url = input.Url;
page.content = input.RawHtml;
//page.setContent(input.RawHtml, input.Url); //this didn't work either


推荐答案

setTimeout制作它工作,即使我没有兴奋等待每页的一定时间。 此处讨论的waitFor方法不起作用,因为我不知道每个页面可能包含哪些元素。

The setTimeout made it work even though I'm not excited to wait a set amount of time for each page. The waitFor approach that is discussed here doesn't work since I have no idea what elements each page might have.

var system = require('system');
var page = require('webpage').create();
page.setContent(input.RawHtml, input.Url);
window.setTimeout(function () {
    console.log(page.content);
    phantom.exit();
}, input.WaitToRenderTimeInMilliseconds);

这篇关于PhantomJS如何在html字符串中呈现javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆