java车次信息_java实现根据起点终点和日期查询去哪儿网的火车车次和火车站点信息...

本文介绍如何从去哪儿网爬取火车票数据,包括站点经纬度转换及车次信息获取的方法。通过分析网站结构,利用JSON数据接口,实现对火车票详细信息的抓取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文章为原创文章,转载请注明,欢迎评论和改正。

一,分析

之前所用的直接通过HTML中的元素值来爬取一些网页上的数据,但是一些比较敏感的数据,很多正规网站都是通过json数据存储,这些数据通过HTML元素是爬取不到的,所以只能通过json数据的api接口来爬取数据。

二,网站处理

1,打开去哪儿网的网站https://train.qunar.com/,找到火车票查询,输入起点终点和日期,查询。

34dd5c17ac0cefdde173b49b8106396f.png

2,右击打开审查元素,点击network

bebc70cde95ac55292a3f34b4a92f5ea.png

3,刷新网页,找到XHR,点击链接

1b0bc658f11c58b71beef488dd7fd31a.png

4,找到s2sBeanList这一属性,打开即为所需要的数据

d9fea3a2ee93664dddd6454b26a394b9.png

header里有所需的地址

40956965849572fd87ca68cc2f8321ac.png

三,项目结构

1,所需jar包,基本是分析json数据的时候用到

a441816b70c71e2c67c12afe668e5f53.png

2,项目目录结构

5eb08796088a613673351379fc5fcc5e.png

四,代码分析

1,解析URL地址,并以将json数据转化为String类型返回

public static String geturl(String url){

StringBuffer json = new StringBuffer();

try {

URL u = new URL(url);

URLConnection yc = u.openConnection();

//读取返回的数据

BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(),"UTF-8"));

String inputline = null;

while((inputline=in.readLine())!=null){

json.append(inputline);

}

in.close();

} catch (Exception e) {

e.printStackTrace();

}

//将StringBuffer转化为json格式的字符串

String jsonStr=json.toString();

//将不规范的字符串形式的json数据规范化

jsonStr=jsonStr.substring(jsonStr.indexOf("{"),jsonStr.length()-1);

return jsonStr;

}

2,将地址转化为经纬度的方法(用到即可取)

public static String getLngLat(String address) {

StringBuffer json = new StringBuffer();

try {

URL u = new URL("http://restapi.amap.com/v3/geocode/geo?address="+address+"&output=JSON&key=7f4ffae4074e8b8e4d147190527a4b72");

URLConnection yc = u.openConnection();

//读取返回的数据

BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(),"UTF-8"));

String inputline = null;

while((inputline=in.readLine())!=null){

json.append(inputline);

}

in.close();

} catch (Exception e) {

e.printStackTrace();

}

String jsonStr=json.toString();

JSONObject jsonObject = JSONObject.fromObject(jsonStr);

//判断是否有这个点

if(jsonObject.getJSONArray("geocodes").size()>0)

return jsonObject.getJSONArray("geocodes").getJSONObject(0).get("location").toString();

else

return null;

}

3,查询火车票的代码

public static List loadTrainRoute(String startPoint,String endPoint,String date) {

String strUrl="https://train.qunar.com/dict/open/s2s.do?callback=jQuery17203917861486539813_1558231852669"

+ "&dptStation="+startPoint+"&arrStation="+endPoint+"&date="+date //日期格式2019-05-20

+ "&type=normal&user=neibu&source=site&start=1&num=500&sort=3&_=1558231852892";

//调用方法将url转为json格式的字符串

String jsonStr=geturl(strUrl);

JSONObject jsonObject = JSONObject.fromObject(jsonStr);

JSONArray jArray=jsonObject.getJSONObject("data").getJSONArray("s2sBeanList");

for(int i=0;i

//循环遍历所有车次

jsonObject=(jArray.getJSONObject(i));

System.out.println("起点:"+startPoint);

System.out.println("起点经纬度:"+getLngLat(startPoint)); //getLngLat 调用将地址转化为经纬度的方法

System.out.println("车次"+jsonObject.get("trainNo").toString());

System.out.println("车站:"+jsonObject.get("dptStationName")+"-"+jsonObject.get("arrStationName"));

System.out.println("时间段:"+jsonObject.get("dptTime")+"-"+jsonObject.get("arrTime"));

System.out.println("一等座的价格:"+jsonObject.getJSONObject("seats").getJSONObject("一等座").get("price"));

System.out.println("一等座的剩余票数:"+jsonObject.getJSONObject("seats").getJSONObject("一等座").get("count"));

}

return null;

}

jsonObject=(jArray.getJSONObject(i));为循环遍历所有车次

6744b9b149851bb1f719521494382d55.png

五,完整代码

package test;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.URL;

import java.net.URLConnection;

import java.util.List;

import com.travel.bean.TrainMess;

import com.travel.util.AddressLngLatExchange;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

public class GetTrainTest {

//解析URL

public static String geturl(String url){

StringBuffer json = new StringBuffer();

try {

URL u = new URL(url);

URLConnection yc = u.openConnection();

//读取返回的数据

BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(),"UTF-8"));

String inputline = null;

while((inputline=in.readLine())!=null){

json.append(inputline);

}

in.close();

} catch (Exception e) {

e.printStackTrace();

}

//将StringBuffer转化为json格式的字符串

String jsonStr=json.toString();

jsonStr=jsonStr.substring(jsonStr.indexOf("{"),jsonStr.length()-1);

return jsonStr;

}

//调用将地址转化为经纬度的方法

public static String getLngLat(String address) {

StringBuffer json = new StringBuffer();

try {

URL u = new URL("http://restapi.amap.com/v3/geocode/geo?address="+address+"&output=JSON&key=7f4ffae4074e8b8e4d147190527a4b72");

URLConnection yc = u.openConnection();

//读取返回的数据

BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(),"UTF-8"));

String inputline = null;

while((inputline=in.readLine())!=null){

json.append(inputline);

}

//System.out.println(json);

in.close();

} catch (Exception e) {

e.printStackTrace();

}

String jsonStr=json.toString();

JSONObject jsonObject = JSONObject.fromObject(jsonStr);

//System.out.println(jsonObject.getJSONArray("geocodes"));

if(jsonObject.getJSONArray("geocodes").size()>0)

return jsonObject.getJSONArray("geocodes").getJSONObject(0).get("location").toString();

else

return null;

}

@SuppressWarnings("null")

public static List loadTrainRoute(String startPoint,String endPoint,String date) {

String strUrl="https://train.qunar.com/dict/open/s2s.do?callback=jQuery17203917861486539813_1558231852669"

+ "&dptStation="+startPoint+"&arrStation="+endPoint+"&date="+date //日期格式2019-05-20

+ "&type=normal&user=neibu&source=site&start=1&num=500&sort=3&_=1558231852892";

//调用方法将url转为json格式的字符串

String jsonStr=geturl(strUrl);

JSONObject jsonObject = JSONObject.fromObject(jsonStr);

JSONArray jArray=jsonObject.getJSONObject("data").getJSONArray("s2sBeanList");

for(int i=0;i

jsonObject=(jArray.getJSONObject(i));

System.out.println("起点:"+startPoint);

System.out.println("起点经纬度:"+getLngLat(startPoint)); //getLngLat 调用将地址转化为经纬度的方法

System.out.println("车次"+jsonObject.get("trainNo").toString());

System.out.println("车站:"+jsonObject.get("dptStationName")+"-"+jsonObject.get("arrStationName"));

System.out.println("时间段:"+jsonObject.get("dptTime")+"-"+jsonObject.get("arrTime"));

System.out.println("一等座的价格:"+jsonObject.getJSONObject("seats").getJSONObject("一等座").get("price"));

System.out.println("一等座的剩余票数:"+jsonObject.getJSONObject("seats").getJSONObject("一等座").get("count"));

}

return null;

}

public static void main(String[] args) {

loadTrainRoute("北京", "上海", "2019-06-10");

}

}

六,火车车次的站点信息查询

1,分析:因为火车车次和改车次的站点数据存储的位置不同,所以车站站点信息要更改URL

2,刷新网页点击一个车次,展开查看经停

50ee9cccf264434f6c2f9f3aad793191.png

3,先点击XHR,然后点击js,查看到改车次的信息

b3731c4cc75a147ddadc36569ecff23d.png

4aeaeeccf5bd21e5b7210762c4062078.png

根据URL道理同上,对json数据进行分析,然后爬取下来。

本文章为原创文章,转载请注明,欢迎评论和改正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值