用Java运行JavaScript
我有一个javaScript标记:
var CurrentDate= new Date(); var JAT= 1; function pz() {}; init(); document.getElementById("cities").selectedIndex=12; coord(); main();
我想在java中运行此脚本并接收服务器响应的html文档。 我怎样才能做到这一点? 我还需要解析收到的文档并提取一些特殊标签。
谢谢。
我知道最简单的方法是使用HtmlUnit。
WebClient htmlunit = new WebClient(); HtmlPage page = htmlunit.getPage("http://www.google.com"); //executing the javascript and getting the new page page = page.executeJavaScript("").getNewPage();
更多信息: http : //www.aviyehuda.com/2011/05/htmlunit-a-quick-introduction/
Sajad / Future访问者,如果使用较旧的java版本(<1.6,更高版本的java嵌入了它),您可以查看Jakarta Bean Scripting Framework(BSF)来运行您的javascript甚至作为独立代码