htmlunit官网简易教程(翻译)

1 环境搭建:

  1)下载

  从链接:http://sourceforge.net/projects/htmlunit/files/htmlunit/

  下载最新的bin文件

  2)关于bin文件

  里面主要包含两部分,一是lib目录下的.jar文件,还有就是apidocs目录下的帮助文件(即API说明文件,打开index-all.html,是以网页形式提供)

  3)配置java的CLASSPATH(纯手工方法)

  将lib目录下的所有.jar文件复制到任意目录(如:c:\htmlunit\lib\)

  然后右击我的电脑->属性->高级->环境变量->系统变量 中,对CLASSPATH进行编辑,如果没有就新建一个(如果运行java或编译时有错误,就在)

  务必将所有.jar文件的详细地址添加到CLASSPATH中,而不是用“c:\htmlunit\lib\”来代替,如.;c:\htmlunit\lib\1.jar;c:\htmlunit\lib\2.jar; 才是正确的写法

  务必每一个都写清楚,需要注意最前面有个点".",最后面有个";"

2 解释和说明:

  1).jar其实就是编译好的.class文件集,可以使用rar解压软件打开。所以.jar本质是一个目录

  2)官网的教程有些地方写的很奇怪和不直观,所以我做了些调整,主要是使输出结果更加直观

  3)里面每一个函数的具体使用方法在APIDOCS中已经有详细的说明了,我这里就不重复了

3 开始翻译教程

3.1 获取页面的TITLE、XML代码、文本

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.WebClientOptions;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlBody;
import java.util.List;

public class helloHtmlUnit{
    public static void main(String[] args) throws Exception{
        String str;
        //创建一个webclient
        WebClient webClient = new WebClient();
        //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();
    }
}

 

3.2 使用不同版本的浏览器打开

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.WebClientOptions;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlBody;
import java.util.List;

public class helloHtmlUnit{
    public static void main(String[] args) throws Exception{
        String str;
        //使用FireFox读取网页
        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/");
        str = page.getTitleText();
        System.out.println(str);
        //关闭webclient
        webClient.closeAllWindows();
    }
}

 

3.3 找到页面中特定的元素

public class helloHtmlUnit{
    public static void main(String[] args) 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();
    }
}

 

3.4 元素检索

public class helloHtmlUnit{
    public static void main(String[] args) 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();
    }
}

 

3.5 提交搜索

public class helloHtmlUnit{
    public static void main(String[] args) 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());
    }
}

 

posted @ 2014-10-13 16:17  布兰姥爷  阅读(44145)  评论(3编辑  收藏  举报