由于最近比较忙时间有限,所以只将代码贴出来.
有时间我会将代码详细注释下.如果有不明白的请留言
其中测试的网站是
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;
}
}
转载请注明作者小刘