HtmlUnit 试用

pom:

<dependency>
            <groupId>net.sourceforge.htmlunit</groupId>
            <artifactId>htmlunit</artifactId>
            <version>2.18</version>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.htmlunit</groupId>
            <artifactId>htmlunit-core-js</artifactId>
            <version>2.9</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>

        <!-- http client -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5</version>
        </dependency>


package htmlunit;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlListItem;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2016/11/17.
 */
public class Main {
    public static void main(String[] args) throws IOException {
        final WebClient webClient=new WebClient(BrowserVersion.CHROME);
        // 启动JS
        webClient.getOptions().setJavaScriptEnabled(true);
        //忽略ssl认证
        //webClient.getOptions().setUseInsecureSSL(true);
        //禁用Css,可避免自动二次请求CSS进行渲染
        //webClient.getOptions().setCssEnabled(false);
        int count = Integer.MAX_VALUE;
        final HtmlPage page=webClient.getPage("http://event.dianping.com/midas/1activities/a4635/index.html");
        count = webClient.waitForBackgroundJavaScript(10000);
//        while(count != 0 ){
//            count = webClient.waitForBackgroundJavaScript(10000);
//            System.out.println(count);
//        }
        //获取所有输出链接
//        for(HtmlAnchor anchor : page.getAnchors()){
//            System.out.println(anchor.getTextContent()+" : "+anchor.getAttribute("href"));
//        }
        final HtmlListItem titles = (HtmlListItem)page.getByXPath("/html/body/div[5]/div[1]/ul/li[2]").get(0);
        System.out.println(titles.getTextContent());
        System.out.println(page.asXml());
        webClient.close();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值