Java crawl 12306 information to achieve the train ticket query sample _java

Source: Internet
Author: User
Tags ticket stringbuffer

Recently in a micro-letter of the public account, involving train ticket inquiries, the previous use of the Internet to find an interface, but only to find the train timetable, 12306 did not provide a special check the interface. Today, I suddenly think of myself go directly to the 12306 query, crawl query returned packets, so you can get train ticket information. Here is the process of obtaining more than 12,306 votes with the notes.

First, I use the Firefox browser 12306 to query the remaining votes. Open the Firefox Web console and select "Record requests and response principals" on the network

Then, after entering the address date information, click the Query button on the Web page to see the address of the Web page request under the Web console:

Is the second in the picture, that is, when you click the Query button, the actual address of the event is processed. Click to open it to see

Request URL, request header, response Head and response subject these things, in response to the main body is the train ticket information we need.

With this request URL, you can go to the actual code to do the operation. You can find that the format of the URL is

The front is the address of the processing request, followed by the parameter purpose_codes refers to the adult ticket (aadult), student ticket (Try it yourself), Querydate is the date, from_station and to_station as the name suggests is the departure station and arrival station. Here Beijing and Wuhan are expressed as BJP and WHN respectively.

In Java code, you can write HTTPS requests directly to get train ticket information packets.

Copy Code code as follows:

public static list<newtrain> getmsg (String startcity,string endcity,int isadult) throws exception{

List<newtrain> trains = new arraylist<newtrain> ();

String sstartcity = Citycode.format (startcity);
String sendcity = Citycode.format (endcity);

Trustmanager[] TM = {new Myx509trustmanager ()};
Sslcontext Sslcontext = sslcontext.getinstance ("SSL", "Sunjsse");
Sslcontext.init (NULL, TM, New Java.security.SecureRandom ());
Get the Sslsocketfactory object from the above Sslcontext object
Sslsocketfactory SSF = Sslcontext.getsocketfactory ();
String type = "adult";
if (Isadult = = 1) {
Type = "0X00";
}

String urlstr = "https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=" +type+ &querydate=2014-04-27&from_ station= "+sstartcity+" &to_station= "+sendcity;

URL url = new URL (urlstr);

Httpsurlconnection con = (httpsurlconnection) url.openconnection ();
Con.setsslsocketfactory (SSF);

InputStreamReader in = new InputStreamReader (Con.getinputstream (), "utf-8");

BufferedReader bfreader = new BufferedReader (in);

StringBuffer sb = new StringBuffer ();

String line = "";

while (line = Bfreader.readline ())!= null) {
Sb.append (line);
}
System.out.println (Sb.tostring ());
}


The Citycode.format () of this code is written to convert the station name in Chinese to a letter combination, and the following lines are about HTTPS requests. The Web site is the Web site just acquired. This code is executed after the output is as follows:

It's easy to see that these data are all JSON data (I've done a simple process to get him to print out the strips).

Since it's JSON data, it's good to do it. By taking out a piece of data for analysis, you can analyze what the key value means. I've only analyzed a couple of key values I need.

Then directly write a train class to store train ticket information, easy to display after the use.

Copy Code code as follows:

public class Newtrain {

Private String To_station_name; Land of arrival

Private String Station_train_code; Train number

Private String From_station_name; Departure

Private String start_time; Departure time

Private String Arrive_time; Arrival time

Private String Lishi; Take time

Private String Zy_num; Number of first-class seats

Private String Ze_num; Number of second-class seats

Private String Swz_num; Number of business seats

Private String Gr_num; Number of advanced soft sleeper

Private String Rw_num; Number of soft sleeper

Private String Rz_num; Number of Soft-seat

Private String Yw_num; Number of hard sleeper

Private String Yz_num; Number of hard seats

Private String Tz_num; Number of principal seats

Private String Wz_num; No seat number
}

The next step is simply to put the JSON data into the train class object.

Well, the basic work is done, and the next task is to integrate the functions into the project.

This is used in the Chinese station name and letter combination of a TXT file (read txt get Chinese station name corresponding to the combination of letters, some may not be full

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.