抓取12306官网余票信息
一、前言:
为了学习cookies的应用,之前听过有人写代码,玩转12306,今天自己也尝试一下,学以致用哈,用12306的数据玩玩。只是抓取余票,功能不完善。
*注意:12306接口信息老是变化,所以,现在写的指不定哪天就不好使了。还有,我是直接把查询接口放在代码里面了,(https://kyfw.12306.cn/otn/leftTicket/queryX?leftTicketDTO.train_date=2017-09- 24&leftTicketDTO.from_station=MDB&leftTicketDTO.to_station=HBB&purpose_co des=ADULT)。返回来的json数据解析后有的不知道是干嘛的,所以我只把有用的 列举出来。
二、代码:
ParseJson.java:
package data;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class ParseJson {
public static void main(String args[]) {
start();
}
public static void start() {
List<String> lists = new ArrayList<String>();
String jsonCont = Get_12306_html.getContent();
JSONObject jb = JSONObject.fromObject(jsonCont);
Pattern p = Pattern.compile("(^[^\\|]).*"); // [a-zA-Z|\\d|\\%].*
JSONObject data = jb.getJSONObject("data");
JSONArray ja = data.getJSONArray("result");
for (Object object : ja) {
Matcher m = p.matcher(object.toString());
if (m.find()) {
lists.add(m.group(0));
}
}
System.out
.println("<<<<____________>>>>>>----------------------------------------");
System.out
.println("车次信息\t出发地\t目的地\t出发时间\t到达时间\t历时\tY\t高级软卧\t软卧\t无座\t硬卧\t硬座\t二等座\t一等座\t商务,特等座");
for (int i = 0; i < lists.size(); i++) {
String temp = lists.get(i);
Pattern tp = Pattern.compile("\\|?[^\\|]*"); // \|?[^\|]* // [^.|\|]?[^\|]*
Matcher m = tp.matcher(temp);
List<String> info = new ArrayList<String>();
while (m.find()) {
info.add(m.group());
}
System.out.println(info.get(3) + "\t" + info.get(6) + "\t"
+ info.get(7) + "\t" + info.get(8) + "\t" + info.get(9)
+ "\t" + info.get(10) + "\t" + info.get(11) + "\t"
+ info.get(21) + "\t" + info.get(23) + "\t" + info.get(26)
+ "\t" + info.get(28) + "\t" + info.get(29) + "\t"
+ info.get(30) + "\t" + info.get(31) + "\t" + info.get(32)+"\n");
/*info.get(3);// 车次信息
info.get(6); // 出发地
info.get(7);// 目的地
info.get(8); // 出发时间
info.get(9); // 到达时间
info.get(10);// 历时
info.get(11); // Y
info.get(21); // 高级软卧
info.get(23); // 软卧
info.get(26); // 无座
info.get(28); // 硬卧
info.get(29); // 硬座
info.get(30); // 二等座
info.get(31);// 一等座
info.get(32); // 商务,特等座
*/ }
}
}
Get_cookies.java:
package data;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Get_cookies {
public static String getc() {
String cookies = null;
try {
URL url = new URL("http://dynamic.12306.cn/otn/board/init");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setInstanceFollowRedirects(true);
con.setRequestMethod("GET");
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36");
Map<String, List<String>> m = con.getHeaderFields();
Set<String> s = m.keySet();
StringBuffer sb = new StringBuffer();
if (s.contains("Set-Cookie")) {
List<String> ls = m.get("Set-Cookie");
for (int i = 0; i < ls.size(); i++) {
String ss = ls.get(i).substring(0,
ls.get(i).indexOf(";") + 1);
sb.append(ss);
}
}
String tt = sb.toString();
if (tt != null) {
cookies = tt.substring(0, tt.length() - 1);
} else {
cookies = null;
System.out.println("cookies is null:");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return cookies;
}
}
Get_12306_html.java:
package data;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
public class Get_12306_html {
public static String getContent(){
String content = null;
try {
trustAllHttpsCertificates();
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: " + urlHostName + " vs. "
+ session.getPeerHost());
return true;
}
};
String cookiess=Get_cookies.getc();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
StringBuffer sb=new StringBuffer();
URL url=new URL("https://kyfw.12306.cn/otn/leftTicket/queryX?leftTicketDTO.train_date=2017-09-24&leftTicketDTO.from_station=MDB&leftTicketDTO.to_station=HBB&purpose_codes=ADULT");
HttpsURLConnection con=(HttpsURLConnection) url.openConnection();
con.setDoInput(true);
con.setInstanceFollowRedirects(true);
con.setRequestMethod("GET");
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty("Referer", "https://kyfw.12306.cn/otn/leftTicket/init");
con.setRequestProperty("Cookie", cookiess);
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36");
con.setRequestProperty("X-Requested-With", "XMLHttpRequest");
if(con.getResponseCode()==200){
InputStream inp=con.getInputStream();
InputStreamReader inps=new InputStreamReader(inp,"UTF-8");
BufferedReader br=new BufferedReader(inps);
String str=null;
while((str=br.readLine())!=null){
sb.append(str);
}
content=sb.toString();
}else{
System.out.println(con.getResponseCode());
content=getContent();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
private static void trustAllHttpsCertificates() throws Exception {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
.getSocketFactory());
}
static class miTM implements javax.net.ssl.TrustManager,
javax.net.ssl.X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
}
}
三、截图:
官网信息:
Java模拟结果:
联系邮箱:xhsgg12302@outlook.com
2017_09_24