清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
HtmlUnit是Java版本的浏览器,没有界面但是可以模拟任何浏览器操作。
下面示例访问htmlunit的官网,并打印网页的标题
import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage; public class Try { public static void main (String args[]) throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); System.out.println(page.getTitleText()); webClient.closeAllWindows(); } }
输出: HtmlUnit - Welcome to HtmlUnit
HtmlUnit提供了getElementById和Xpath方式查找界面上的元素,如下示例:
HtmlDivision div=(HtmlDivision)page.getElementById("hed");
HtmlDivision div=(HtmlDivision)page.getByXPath("//div").get();