htmlUtil 网页爬取工具

时间:2023-03-09 14:50:09
htmlUtil 网页爬取工具

Htmlunit是一款模拟浏览抓取页面内容的Java框架,具有js解析引擎(rhino),可以解析页面的js脚本,得到完整的页面内容,特殊适合于这种非完整页面的站点抓取

 /**
      * 获取页面的TITLE、XML代码、文本
      */
     @Test
     public void connNet() throws Exception {
         String str;
         // 创建一个webclient
         WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
         // htmlunit 对css和javascript的支持不好,所以请关闭之
         webClient.getOptions().setJavaScriptEnabled(false);
         webClient.getOptions().setCssEnabled(false);
         // 获取页面
         HtmlPage page = webClient.getPage("http://www.baidu.com/");
         // 获取页面的TITLE
         str = page.getTitleText();
         System.out.println(str);
         // 获取页面的XML代码
         str = page.asXml();
         System.out.println(str);
         // 获取页面的文本
         str = page.asText();
         System.out.println(str);
         // 关闭webclient
         webClient.closeAllWindows();
     }

     /**
      * 找到页面中特定的元素
      */
     @Test
     public void getInput() throws Exception {
         // 创建webclient
         WebClient webClient = new WebClient(BrowserVersion.CHROME);
         // htmlunit 对css和javascript的支持不好,所以请关闭之
         webClient.getOptions().setJavaScriptEnabled(false);
         webClient.getOptions().setCssEnabled(false);
         HtmlPage page = (HtmlPage) webClient.getPage("http://www.baidu.com/");
         // 通过id获得"百度一下"按钮
         HtmlInput btn = (HtmlInput) page.getHtmlElementById("su");
         System.out.println(btn.getDefaultValue());
         // 关闭webclient
         webClient.closeAllWindows();
     }

     /**
      * 元素检索
      */
     @Test
     public void search() throws Exception {
         // 创建webclient
         WebClient webClient = new WebClient(BrowserVersion.CHROME);
         // htmlunit 对css和javascript的支持不好,所以请关闭之
         webClient.getOptions().setJavaScriptEnabled(false);
         webClient.getOptions().setCssEnabled(false);
         HtmlPage page = (HtmlPage) webClient.getPage("http://www.baidu.com/");
         // 查找所有div
         List<?> hbList = page.getByXPath("//div");
         HtmlDivision hb = (HtmlDivision) hbList.get(0);
         System.out.println(hb.toString());
         // 查找并获取特定input
         List<?> inputList = page.getByXPath("//input[@id='su']");
         HtmlInput input = (HtmlInput) inputList.get(0);
         System.out.println(input.toString());
         // 关闭webclient
         webClient.closeAllWindows();
     }

     /**
      * 提交搜索
      */
     @Test
     public void submitForm() throws Exception {
         // 创建webclient
         WebClient webClient = new WebClient(BrowserVersion.CHROME);
         // htmlunit 对css和javascript的支持不好,所以请关闭之
         webClient.getOptions().setJavaScriptEnabled(false);
         webClient.getOptions().setCssEnabled(false);
         HtmlPage page = (HtmlPage) webClient.getPage("http://www.baidu.com/");
         // 获取搜索输入框并提交搜索内容
         HtmlInput input = (HtmlInput) page.getHtmlElementById("kw");
         System.out.println(input.toString());
         input.setValueAttribute("博客园");
         System.out.println(input.toString());
         // 获取搜索按钮并点击
         HtmlInput btn = (HtmlInput) page.getHtmlElementById("su");
         HtmlPage page2 = btn.click();
         // 输出新页面的文本
         System.out.println(page2.asText());
     }