htmlunit默认是会对网页中的css,javascript解析的,对于一般的非Js加载页面采集,我们可以把css.javascript解析去掉,这样可以提高效率;但是对于Js加载的页面,就不能去掉了;
我们给下演示代码:
package com.hbk.htmlunit;
import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlListItem;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
/**
*
* @author 黄宝康 获取http://www.3dns.com.cn的源代码
*/
public class HtmlUnitTest {
public static void main(String[] args) {
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_45,"118.212.137.135",31288); // 实例化Web客户端
webClient.getOptions().setCssEnabled(false);// 取消css支持
webClient.getOptions().setJavaScriptEnabled(false);// 取消JavaScript的支持
try {
HtmlPage page = webClient.getPage("http://www.3dns.com.cn/");
System.out.println("网页html:"+page.asXml());// 获取Html
System.out.println("===================");
System.out.println("网页文本:"+page.asText());// 获取文本
} catch (FailingHttpStatusCodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
webClient.close(); // 关闭客户端,释放内存
}
}
}