博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用httpclient抓取全国火车票信息
阅读量:4183 次
发布时间:2019-05-26

本文共 4522 字,大约阅读时间需要 15 分钟。

一个通过httpclient抓取火车票信息的程序,需要修改下才能跑通,需要自己封装下httpclient,然后用get方式调用,还有fastJson,需要自己去解析下获得的数据,catchTrainInfo()是入口方法

import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import javax.annotation.Resource;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.Set;public class CatchTrainInfo {
/** * 抓取火车票信息 * @param attachmentId * @return */ @SuppressWarnings("unchecked") public String catchTrainInfo() { //车站信息 HashSet
station = new HashSet
(); //临时车站信息 HashSet
stationTmp = new HashSet
(); //车次信息 Map
train = new HashMap
(); String stationHtml = HttpClientUtil.get("http://www.59178.com/zhan/"); String stationName = ""; //截取车站信息 String stations[] = stationHtml.split("htm'>"); for (int i = 0; i < stations.length; i++) { if (i == 0 ) { continue; } stationName = getStation(stations[i]); if("".equals(stationName)) { continue; } station.add(stationName); } //循环,根据车站信息获取车次信息 getTrainsByStation(station,train); //循环,根据车次获取车次详情,并保存入库 getTrainDetailsByTrains(stationTmp,train); //继续执行3次循环,(本来应该stationTmp.size()==0的时候,防止无限循环) int loop = 3; while (loop > 0) { //stationTmp 和 station比较,stationTmp去掉已经遍历过的站点,station用stationTmp替代,继续循环 HashSet
stationTmp1 = (HashSet
) stationTmp.clone(); for (String stationTmpElement : stationTmp) { for (String stationElement : station) { if(stationTmpElement.equals(stationElement)) { stationTmp.remove(stationTmpElement); } } } station = stationTmp1; //循环,根据车站信息获取车次信息 getTrainsByStation(stationTmp,train); //循环,根据车次获取车次详情,并写入数据库 getTrainDetailsByTrains(stationTmp,train); loop--; }return "success!"; } /** * 根据车次获取车次详情,并写入数据库 * @param stationTmp * @param train */ private void getTrainDetailsByTrains(HashSet
stationTmp, Map
train) { Iterator
> iterator = train.entrySet().iterator(); while (iterator.hasNext()) { Entry
entry = iterator.next(); String key = entry.getKey(); String value = entry.getValue(); if("unused".equals(value)) { getAndSaveTrainsDetails(key,stationTmp); entry.setValue("used"); } try { //休眠一会,防止反爬虫Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();} }}/** * 根据车站信息获取车次信息 * @param station * @param train */ private void getTrainsByStation(HashSet
station, Map
train) { Iterator
iterator = station.iterator(); while (iterator.hasNext()) { String key = iterator.next(); getTrainsInfo(train,key); try { //休眠一会,防止反爬虫Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();} }}/*** 根据车站信息得到车次信息* @param train* @param stationName * @return*/private void getTrainsInfo(Map
train, String stationName) {try {stationName = URLEncoder.encode(stationName,"utf-8");} catch (UnsupportedEncodingException e1) {log.error("转码出错了!stationName:" + stationName);} String url = "http://train.qunar.com/qunar/stationInfo.jsp?q=" + stationName + "&format=json"; //根据车站信息获取车次 ticketInfo String ticketInfo = HttpClientUtil.get(url); try{ @SuppressWarnings("unchecked")java.util.Map
ticketInfos = (Map
) JSONObject.parseObject(ticketInfo, java.util.Map.class).get("ticketInfo"); Set
> entries = ticketInfos.entrySet( ); if (entries != null) { Iterator
> iterator = entries.iterator( ); while (iterator.hasNext( )) { Entry
entry = iterator.next(); String key = (String) entry.getKey( ); String trains[] = key.split("/"); for(int i = 0; i< trains.length; i++) {train.put(trains[i], "unused"); } } } } catch (Exception e) { log.info("根据车站信息得到车次信息报错:"+e.getMessage()); }}/*** 解析得到车站信息* @param str* @return*/private String getStation(String str) {if (str == null || str.length() <= 0) {return "";}int pos = -1;pos = str.indexOf("", pos + 1);if (pos == -1) {return "";}return str.substring(0, pos);}/*** 根据车次获取车次详情,并保存入库* * @param key* @param stationTmp*/ public void getAndSaveTrainsDetails(String key, HashSet
stationTmp) {String url = "http://train.qunar.com/qunar/checiInfo.jsp?q=" + key + "&date=20170107&format=json";String trainScheduleBody = HttpClientUtil.getUtf8(url);try {List
ticketInfos = (List) JSONObject.parseObject(trainScheduleBody, Map.class).get("trainScheduleBody");for (int i = 0; i < ticketInfos.size(); i++) {List
list = (List
) JSONObject.parseObject(ticketInfos.get(i).toString(), Map.class).get("content");if (list == null || list.size() <= 0) {continue;}stationTmp.add(list.get(1));// 得到详情,更新入库//TODO }} catch (Exception e) {log.info("根据车次获取车次详情报错:" + e.getMessage());}}}

转载地址:http://fqzoi.baihongyu.com/

你可能感兴趣的文章
实模式,保护模式与V86模式
查看>>
628. Maximum Product of Three Numbers(排序)
查看>>
Linux内核-------同步机制(二)
查看>>
面试题31-------连续子数组的最大和(数组)
查看>>
epoll 实现Chat
查看>>
21. Merge Two Sorted Lists(链表)
查看>>
2. Add Two Numbers(链表)
查看>>
637. Average of Levels in Binary Tree(Tree)
查看>>
226. Invert Binary Tree(Tree)
查看>>
328. Odd Even Linked List(链表)
查看>>
199. Binary Tree Right Side View(Tree)
查看>>
230. Kth Smallest Element in a BST(Tree)
查看>>
求字符串的最长回文串-----Manacher's Algorithm 马拉车算法
查看>>
回溯法常用的解题模板和常见题型
查看>>
深入分析Java I/O 的工作机制
查看>>
动态规划的套路----左神
查看>>
KMP算法简解
查看>>
左神算法课进阶版总结
查看>>
左神算法基础班总结
查看>>
Linux性能优化
查看>>