在servlet生成的网页上使用HTMLUnit [英] Using HTMLUnit on a webpage generated by a servlet
问题描述
如何使用HTMLUnit从Java servlet生成的网页中获取数据.尝试阅读网页时,我总是收到错误消息. /getSurvey是创建网页的servlet,但是如何访问从servlet生成的HTML.
How could I use HTMLUnit to get data from a webpage generated by a java servlet. I keep getting an error when I try to read the webpage. /getSurvey is the servlet that creates the webpage but how can I access the HTML generated from the servlet.
最终的WebClient webClient = new WebClient(); 最终的HtmlPage页面= webClient.getPage("http://survey-creator.appspot.com/getSurvey");
final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://survey-creator.appspot.com/getSurvey");
推荐答案
HtmlUnit并不是真正的公正". HTML解析器.这是一种程序化的网络浏览器.旨在使用Java语言以编程方式浏览网页和/或填写Web表单.如果您的唯一目的是将HTML作为字符串获取,请使用真正的HTML解析器.为此,我可以推荐 Jsoup .
HtmlUnit is not really "just" a HTML parser. It's kind of a programmatic webbrowser. It's intented to surf through web pages and/or fill out web forms programmatically using Java language. If your sole purpose is to get the HTML as a String, use a real HTML parser. I can recommend Jsoup for this.
String html = Jsoup.connect("http://stackoverflow.com").get().html();
就是这样.但是,它可以做的还远远不止这些,例如选择感兴趣的元素.
That's it. It can however do much more than that, such as selecting elements of interest.
这篇关于在servlet生成的网页上使用HTMLUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!