httpclient(get)+jsoup获取身份证归属地信息

由于最近比较忙时间有限,所以只将代码贴出来.

有时间我会将代码详细注释下.如果有不明白的请留言



其中测试的网站是


http://qq.ip138.com/idsearch/



测试的身份证号码:652302197607051677


Java代码:

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Scanner;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;


public class IdCard {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
		//创建httpclient客户端
		HttpClient client=new HttpClient();
		
		//设置client主机地址和端口
		client.getHostConfiguration().setHost( "qq.ip138.com" , 80, "http" );
		
		String url="/idsearch/index.asp";
		
		Scanner in = new Scanner(System.in);
		System.out.print("身份证号码为:");
		String ID=in.next();
		System.out.print(ID);
		String params = "action=idcard&"+"userid="+ID+"&B1=%B2%E9+%D1%AF";
	
		
		HttpMethod method = getMethod(url, params);
		
		
		client.executeMethod(method);  
		
		
		//String response = method.getResponseBodyAsString();  
        String response = new String(method.getResponseBodyAsString().getBytes("ISO-8859-1"));                  
        System.out.println(response);  
        
        
        Document doc=Jsoup.parse(response);
        
      //Elements  是 Element 的集合类
        Elements element=doc.select("table");  //从加载的信息中查找table 标签
        
        
        
        //从查找到table属性的Elements集合中获取标签 tr 或者tr[class$=alt] 表示 tr标签内class属性=alt
//      Elements titleName=element.select("tr[class$=alt]");   
        Elements titleName=element.select("tr");
        for(org.jsoup.nodes.Element name:titleName){
           System.out.println(name.text());
        }
        
	}
	
	
	 private static HttpMethod getMethod(String url,String params) throws IOException{  
	        GetMethod get = new GetMethod(url+"?"+params);  
	        get.releaseConnection();  
	        return get;  
	    }  

}



结果截图如下:还有一些小bug,没时间调,等有时间再改吧.



转载请注明作者小刘







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值