获取浏览器呈现的 html+javascript [英] get a browser rendered html+javascript

查看:19
本文介绍了获取浏览器呈现的 html+javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个命令行工具(或 Javascript/PHP,但我认为命令行是一种方法)来渲染和获取 URL 的渲染内容,但重要的是我需要渲染 Javascript 不仅是 CSS/Html/图片.

I need a comandline tool (or Javascript/PHP, but i think commandline is the one way) for render and get the rendered content of URL, but the important its I need to renderer the Javascript not only the CSS/Html/images.

例如像这样的命令:renderengine http://www.google.es outputfile.html"和网页内容(解析的 html 和执行的 javascript)保存在 outputfile.html 中.

For example command like: "renderengine http://www.google.es outputfile.html" and the content of the web (parsed html and javascript executed) isa saved in outputfile.html.

我需要这个,因为我需要获取一个完整的 javascript 网站的结果,如grooveshark,该网站全部使用 javascript/ajax 加载,而爬虫什么也没找到,只有基本的 HTML 空模板(因为在使用 ajax/javscript 后加载)

I need this because i need to take the result of a full javascript website like grooveshark, the site load all using javascript/ajax and the crawlers dont find nothing, only basic HTML empty template (because is loaded after using ajax/javscript)

是否存在任何支持 Javascript(例如 V8)的 linux 浏览器引擎,输出结果以保存在文件中?

Exists any browser engine for linux with support to Javascript (for example V8) that output the result for save in files?

推荐答案

www.phantomjs.org 尝试 phantomjs并且您可以轻松修改包含的 rasterize.js 以导出呈现的 HTML.它基于 webkit 并对目标站点的 javascript 进行全面评估,允许您根据需要调整超时或首先执行自己的代码.我个人用它来保存完全渲染的 Knockout.js 模板的硬拷贝 HTML 文件版本.

Try phantomjs from www.phantomjs.org and you can easily modify the included rasterize.js to export the rendered HTML. It's based on webkit and does full evaluation of your target site's javascript, allowing you to adjust timeouts or execute your own code first if you wish. I personally use it to save hardcopy HTML file version of fully-rendered knockout.js templates.

它执行 javascript,所以我只是做了这样的事情并将控制台输出保存到一个文件中:

It executes javascript so I just did something like this and saved the console output to a file:

var markup = page.evaluate(function(){return document.documentElement.innerHTML;});
console.log(markup);
phantom.exit();

这篇关于获取浏览器呈现的 html+javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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