使用 Javascript 获取最终 HTML 将 Java 呈现为字符串 [英] Getting Final HTML with Javascript rendered Java as String
问题描述
我想从 HTML 页面中获取数据(抓取它).但它包含 javascript 中的评论.在普通的 java url fetch 中,我只得到 HTML(实际的),而没有执行 Javascript.我想要执行 Javascript 的最终页面.
I want to fetch data from an HTML page(scrape it). But it contains reviews in javascript. In normal java url fetch I am only getting the HTML(actual one) without Javascript executed. I want the final page with Javascript executed.
示例:- http://www.glamsham.com/movies/reviews/rowdy-rathore-movie-review-cheers-for-rowdy-akki-051207.asp
此页面有作为 facebook 插件的评论,这些评论是作为 Javascript 获取的.
This page has comments as a facebook plugin which are fetched as Javascript.
即使在此也与此类似.http://www.imdb.com/title/tt0848228/reviews
我该怎么办?
推荐答案
使用phantomjs:http://phantomjs.org
var page = require('webpage').create();
page.open("http://www.glamsham.com/movies/reviews/rowdy-rathore-movie-review-cheers-for-rowdy-akki-051207.asp")
setTimeout(function(){
// Where you want to save it
page.render("screenshoot.png")
// You can access its content using jQuery
var fbcomments = page.evaluate(function(){
return $(".fb-comments iframe").contents().find(".postContainer")
})
},10000)
您必须使用 phantom --web-security=no
中的选项来允许跨域交互(即对于 facebook iframe)
You have to use the option in phantom --web-security=no
to allow cross-domain interaction (ie for facebook iframe)
要从 phantomjs 与其他应用程序通信,您可以使用 Web 服务器或发出 POST 请求:https://github.com/ariya/phantomjs/blob/master/examples/post.js
To communicate with other applications from phantomjs you can use a web server or make a POST request: https://github.com/ariya/phantomjs/blob/master/examples/post.js
这篇关于使用 Javascript 获取最终 HTML 将 Java 呈现为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!