1 package zeze; 2 3 import java.io.IOException; 4 5 import org.json.JSONArray; 6 import org.json.JSONException; 7 import org.json.JSONObject; 8 import org.jsoup.Connection; 9 import org.jsoup.Jsoup; 10 import org.jsoup.nodes.Document; 11 import org.jsoup.select.Elements; 12 13 14 /** 15 * 16 * @ClassName: Tiantian 17 * @Description: 快递100天天快递查询 18 * @author zeze 19 * @date 2015年11月2日 下午2:28:46 20 * 21 */ 22 public class Tiantian { 23 24 public static void main(String[] args) throws IOException, JSONException { 25 String url = "http://www.kuaidi100.com/query?type=tiantian&postid=666198673845&id=1&valicode=&temp=0.42161923577077687";// 获取地址 26 int num = 0; 27 // 设置查询参数 28 for (int j = 0; j < 1; j++) { 29 30 Connection conn = Jsoup.connect(url); 31 String key = "666198673845"; 32 String b = key.substring(0, 5); 33 String c = key.substring(5, 12); 34 35 int d = Integer.parseInt(c); 36 key = b + (d + j);// 生成快递单号 37 conn.data("postid", key);// 设置关键字查询字段 38 39 Document doc = null; 40 doc = conn.timeout(100000).get();// 设置请求类型为post型,超时100000毫秒 41 42 Elements results = null; 43 System.out.println("天天快递单号:" + key + " 当前查询次数:" + (j + 1)); 44 results = doc.select("body");// 处理返回数据 45 String json = results.text();// 获取快递状态信息 46 System.out.println(json); 47 JSONObject obj = new JSONObject(json); 48 //JSONArray jsonArray=new JSONArray(json); 49 System.out.println("订单号(nu):"+obj.getString("nu")); 50 System.out.println("查询的快递公司代码(com):"+obj.getString("com")); 51 System.out.println("查询结果状态(status):"+obj.getString("status")); 52 System.out.println("快递单当前的状态(state) :"+obj.getString("state")); 53 System.out.println("数据(data):"+obj.getString("data")); 54 55 JSONArray jsonArray=new JSONArray(obj.getString("data")); 56 for(int i=0;i<jsonArray.length();i++) 57 { 58 JSONObject jsonObject = jsonArray.getJSONObject(i);//时间 59 String context = jsonObject.getString("context"); 60 String time = jsonObject.getString("time"); 61 System.out.println(time+" "+context); 62 } 63 64 65 } 66 System.out.println("本次成功查询到的订单数为:" + num); 67 68 } 69 }
1 package zeze; 2 3 import java.io.IOException; 4 5 import org.json.JSONArray; 6 import org.json.JSONException; 7 import org.json.JSONObject; 8 import org.jsoup.Connection; 9 import org.jsoup.Jsoup; 10 import org.jsoup.nodes.Document; 11 import org.jsoup.select.Elements; 12 13 import Dao.MysqlDao; 14 import bean.Info; 15 16 /** 17 * 18 * @ClassName: Tiantian 19 * @Description: 快递100天天快递查询 20 * @author zeze 21 * @date 2015年11月2日 下午2:28:46 22 * 23 */ 24 public class Tiantian { 25 26 public static void main(String[] args) throws IOException, JSONException { 27 28 int num = 0; 29 // 设置查询参数 30 for (int j = 0; j < 200; j++) { 31 32 String key = "666198673945"; 33 String b = key.substring(0, 5); 34 String c = key.substring(5, 12); 35 int d = Integer.parseInt(c); 36 key = b + (d + j);// 生成快递单号 37 38 String url = "http://www.kuaidi100.com/query?type=tiantian&postid=" + key + "";// 获取地址 39 Connection conn = Jsoup.connect(url);// 连接 40 conn.data("postid", key);// 设置关键字查询字段 41 42 Document doc = null; 43 doc = conn.timeout(100000).get();// 设置请求类型为post型,超时100000毫秒 44 45 Elements results = null; 46 results = doc.select("body");// 处理返回数据 47 String json = results.text();// 获取快递状态信息 48 49 JSONObject obj = new JSONObject(json); // 转为为json对象 50 int statu=Integer.parseInt(obj.getString("status")); 51 if ( statu== 201) { 52 System.out.println("快递单号:" + key + " 当前查询次数:" + (j + 1)); 53 System.out.println(obj.getString("message")); 54 continue; 55 } 56 else { 57 if (obj.getString("nu").isEmpty()) {// 订单号不存在 58 continue; 59 } 60 else { 61 num++; 62 } 63 System.out.println("快递单号(nu):" + obj.getString("nu") + " 当前查询次数:" + (j + 1)); 64 System.out.println("查询的快递公司代码(com):" + obj.getString("com")); 65 System.out.println("查询结果状态(status):" + obj.getString("status")); 66 System.out.println("快递单当前的状态(state) :" + obj.getString("state")); 67 68 int count = 0; 69 JSONArray jsonArray = new JSONArray(obj.getString("data")); 70 for (int i = 0; i < jsonArray.length(); i++) { 71 JSONObject jsonObject = jsonArray.getJSONObject(i);// 时间 72 String context = jsonObject.getString("context"); 73 String time = jsonObject.getString("time"); 74 System.out.println(time + " " + context); 75 76 Info info = new Info(); 77 info.setVar_id(key); 78 info.setVar_infoid(count); 79 info.setVar_time(time); 80 info.setVar_site(context); 81 82 count++; 83 MysqlDao mysqlDao = new MysqlDao();// 插入数据库 84 mysqlDao.Addinfo(info); 85 } 86 87 System.out.println("\n/*****************************************/\n"); 88 } 89 System.out.println("本次成功查询到的订单数为:" + num); 90 } 91 } 92 }