用c 解析html表格,如何使用Java中HttpClient解析Html中的table

1、打开MyEclipse新建一个Java Project 输入名称XXX(httpClientTest)

71771256812c985b27e755daec681c1d.png

2、打开地址:http://hc.apache.org/downloads.cgi,下载相应的jar包

e45d26814805bdfd2105be6912f65108.png

3、打开新建的项目新建lib文件夹,并导入之前下载的jar包,右键项目选择Bulid Path--Configure Bulid Path--Libraries--Add JARs 导入lib中的jar如图所示

07920fc15210982b5773e81ff205d4c9.png

4、新建ClientTest及ClientPojo类。部分代码如下:(这里需要解析Html所以用到了jsoup,可自行上网下载导入jar包方式如上一步骤)

测试地址我选择的是:http://www.live.chinacourt.org/fygg/index/kindid/5.shtml,可根据自己项目需要自行设置。

e6c77e986d047c1feec241d836e3d610.png

12edef661df8e936e03f2972a4cbb85c.png

9fc7ce281a5653667e168ee9276c5fca.png

e9e92c7c636038f8ec0b2d00e2314403.png

package com.gsoft.getnotice;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.http.util.EntityUtils;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;

public class HttpClient {

public ArrayList> getNoticeList(String url) {

HttpClientBuilder  httpClientBuilder =HttpClientBuilder.create();

CloseableHttpClient closeableHttpClient=httpClientBuilder.build();

//String url="http://www.live.chinacourt.org/fygg/index/kindid/5.shtml";

//进行url设置

//String url="http://localhost:8090/httpclient-getnotice/";

HttpGet httpGet= new HttpGet(url);

ArrayList> lists = new ArrayList>();

try {

/*String urlheader="http://www.live.chinacourt.org";

List clientPojo = new ArrayList();

List clientPojoTexts = new ArrayList();*/

HttpResponse httpResponse =closeableHttpClient.execute(httpGet);

HttpEntity entity =httpResponse.getEntity();

if(null!=entity){

//设置编码格式

String response=EntityUtils.toString(entity, "GBK");

//转换Document对象

Document doc=Jsoup.parse(response);

//根据类名获取对象

Elements tables=doc.getElementsByClass("xian");

//Elements elenent=doc.select("table");

Elements tr=tables.select("tr");

for (int i = 0; i < tr.size(); i++) {

//String[] strings= new String[4];

//String[] tds= new String[4];

Element trs=tr.get(i);

//Elements href=trs.select("a");

Elements td=trs.select("td");

//将td值存入list中

ArrayList list = new ArrayList();

for (int j = 0; j < td.size(); j++) {

if(j!=0){

Element tdpojo=td.get(j);

//tds[j]=tdpojo.text();

list.add(tdpojo.text());

}

//System.out.println("---"+tdpojo.text());

}

lists.add(list);

}

//System.out.println(tr);

}

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

closeableHttpClient.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return lists;

}

}

http://jingyan.baidu.com/article/22fe7ced2741043002617f1c.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值