首页 > 代码库 > 浅谈HtmlUnit的使用

浅谈HtmlUnit的使用

htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容。项目可以模拟浏览器运行,被誉为java浏览器的开源实现。这个没有界面的浏览器,运行速度也是非常迅速的。

二、下载地址:http://sourceforge.net/projects/htmlunit/?source=directory 

三、访问指定页面

  网络爬虫第一个要面临的问题,就是如何抓取网页,抓取其实很容易,没你想的那么复杂,一个开源HtmlUnit包,4行主要代码就OK啦!

 1 import java.io.IOException; 2 import java.net.MalformedURLException; 3 import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; 4 import com.gargoylesoftware.htmlunit.WebClient; 5 import com.gargoylesoftware.htmlunit.html.HtmlPage; 6  7 public class Main { 8  9     public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {10         // TODO Auto-generated method stub11         final WebClient mWebClient = new WebClient();12         final HtmlPage mHtmlPage = mWebClient.getPage("http://www.baidu.com");13         System.out.println(mHtmlPage.asText());14         mWebClient.closeAllWindows();15     }16 17 }

运行结果:

技术分享
 1 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError 2 严重: runtimeError: message=[An invalid or illegal selector was specified (selector: ‘:checked‘ error: Invalid selector: *:checked).] sourceName=[http://s1.bdstatic.com/r/www/cache/static/jquery/jquery-1.10.2.min_f2fb5194.js] line=[14] lineSource=[null] lineOffset=[0] 3 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError 4 严重: runtimeError: message=[An invalid or illegal selector was specified (selector: ‘:enabled‘ error: Invalid selector: *:enabled).] sourceName=[http://s1.bdstatic.com/r/www/cache/static/jquery/jquery-1.10.2.min_f2fb5194.js] line=[14] lineSource=[null] lineOffset=[0] 5 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError 6 严重: runtimeError: message=[The data necessary to complete this operation is not yet available.] sourceName=[http://s1.bdstatic.com/r/www/cache/static/jquery/jquery-1.10.2.min_f2fb5194.js] line=[10] lineSource=[null] lineOffset=[0] 7 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 8 警告: CSS error: ‘http://www.baidu.com/‘ [1:81] Error in expression. (Invalid token ";". Was expecting one of: <S>, <NUMBER>, "inherit", <IDENT>, <STRING>, <PLUS>, <HASH>, <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <PERCENTAGE>, <DIMENSION>, <URI>, <FUNCTION>, "-".) 9 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error10 警告: CSS error: ‘http://www.baidu.com/‘ [1:143] Error in style rule. (Invalid token "*". Was expecting one of: <EOF>, <S>, <IDENT>, "}", ";".)11 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning12 警告: CSS warning: ‘http://www.baidu.com/‘ [1:143] Ignoring the following declarations in this rule.13 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error14 警告: CSS error: ‘http://www.baidu.com/‘ [1:339] Error in expression. (Invalid token ";". Was expecting one of: <S>, <NUMBER>, "inherit", <IDENT>, <STRING>, <PLUS>, <HASH>, <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <PERCENTAGE>, <DIMENSION>, <URI>, <FUNCTION>, "-".)15 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error16 警告: CSS error: ‘http://www.baidu.com/‘ [2:204] Error in declaration. (Invalid token "normal". Was expecting one of: <S>, ":".)17 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error18 警告: CSS error: ‘http://www.baidu.com/‘ [2:970] Error in style rule. (Invalid token "*". Was expecting one of: <EOF>, <S>, <IDENT>, "}", ";".)19 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning20 警告: CSS warning: ‘http://www.baidu.com/‘ [2:970] Ignoring the following declarations in this rule.21 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error22 警告: CSS error: ‘http://www.baidu.com/‘ [4:856] Error in style rule. (Invalid token "*". Was expecting one of: <EOF>, <S>, <IDENT>, "}", ";".)23 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning24 警告: CSS warning: ‘http://www.baidu.com/‘ [4:856] Ignoring the following declarations in this rule.25 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error26 警告: CSS error: ‘http://www.baidu.com/‘ [4:1016] Error in style rule. (Invalid token "*". Was expecting one of: <EOF>, <S>, <IDENT>, "}", ";".)27 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning28 警告: CSS warning: ‘http://www.baidu.com/‘ [4:1016] Ignoring the following declarations in this rule.29 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error30 警告: CSS error: ‘http://www.baidu.com/‘ [5:68] Error in style rule. (Invalid token "*". Was expecting one of: <EOF>, <S>, <IDENT>, "}", ";".)31 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning32 警告: CSS warning: ‘http://www.baidu.com/‘ [5:68] Ignoring the following declarations in this rule.33 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error34 警告: CSS error: ‘http://www.baidu.com/‘ [6:751] Error in style rule. (Invalid token "*". Was expecting one of: <EOF>, <S>, <IDENT>, "}", ";".)35 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning36 警告: CSS warning: ‘http://www.baidu.com/‘ [6:751] Ignoring the following declarations in this rule.37 二月 03, 2015 11:46:02 上午 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error38 警告: CSS error: ‘http://www.baidu.com/‘ [8:127] Error in expression; ‘:‘ found after identifier "progid".39 二月 03, 2015 11:46:03 上午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify40 警告: Obsolete content type encountered: ‘text/javascript‘.41 二月 03, 2015 11:46:03 上午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify42 警告: Obsolete content type encountered: ‘text/javascript‘.43 百度一下,你就知道44 百度一下45 新闻hao123地图视频贴吧登录设置更多产品46 把百度设为主页关于百度About Baidu47 ©2015 Baidu 使用百度前必读 京ICP证030173号 
运行结果

 在上面的程序运行的过程中,我们可以得到百度首页的所有内容,上面的代码在运行的过程中会出现很多的警告,出现这些警告的主要原因是由于以下两种原因:

1、HtmlUnit对Javascript的支持不是很好
2、HtmlUnit对CSS的支持不是很好

  明白了上面的两点后,将代码重新改写一下,该禁用的就禁用,同时禁用一些不必要的功能,也有利于提高程序的运行效率,再者说网络爬虫也不需要CSS的支持。

 1 import java.io.IOException; 2 import java.net.MalformedURLException; 3 import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; 4 import com.gargoylesoftware.htmlunit.WebClient; 5 import com.gargoylesoftware.htmlunit.html.HtmlPage; 6  7 public class Main { 8  9     public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {10         // TODO Auto-generated method stub11         final WebClient mWebClient = new WebClient();12         mWebClient.getOptions().setCssEnabled(false);13         mWebClient.getOptions().setJavaScriptEnabled(false);14         final HtmlPage mHtmlPage = mWebClient.getPage("http://www.baidu.com");15         System.out.println(mHtmlPage.asText());16         mWebClient.closeAllWindows();17     }18 19 }
技术分享
 1 百度一下,你就知道 2 搜索设置 | 登录 3 新 闻 网 页 贴 吧 知 道 MP3 图 片 视 频 地 图 4 百度一下 5 输入法 6 手写 7 拼音 8 关闭 9 空间 百科 hao123 | 更多>>10 把百度设为主页11 加入百度推广 | 搜索风云榜 | 关于百度 | About Baidu12 ©2014 Baidu 使用百度前必读 京ICP证030173号
运行结果

++++++++++++++++

 

 

 

 

 

 

 

浅谈HtmlUnit的使用