Java抓取去哪網景點門票數據

比如想獲得去哪網北京所有門票的信息,地址是http://piao.qunar.com/ticket/list.htm?keyword=%E5%8C%97%E4%BA%AC&region=&from=mps_search_suggest,用谷歌瀏覽器訪問,然後按F12,選擇network,經過分析,門票的數據返回list.json,所以可以在搜索框中數據list.json,只查看list.json請求的地址和返回的數據

如果想看返回的json格式點擊Response,或者將Request URL的地址輸入到瀏覽器查看

知道請求的地址和返回的數據格式就可以寫代碼了,實際上就是解析json的過程

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

/**
 * 去哪網景點門票
 */
public class QunarTest {
	public static void main(String[] args) {
		for (int i = 1; i <= 50; i++) {//循環頁,通過谷歌瀏覽器查看總頁數
			String url = "http://piao.qunar.com/ticket/list.json?keyword=%E5%8C%97%E4%BA%AC&region=&from=mps_search_suggest&total=2288&page="+ i;
			String json = openUrl(url, "utf-8");
			JSONObject jsonMap = new JSONObject();
			Map map = jsonMap.fromObject(json);
			Map sightListMap = (Map) map.get("data");
			List<Map<String, Object>> list = (List) sightListMap.get("sightList");
			for (Map itemMap : list) {
				String sightId = itemMap.get("sightId").toString();
				String sightName = itemMap.get("sightName").toString();
				String address = itemMap.get("address").toString();
				String price = itemMap.get("qunarPrice").toString();
				// 景點詳細頁URL
				String detailUrl = "http://piao.qunar.com/ticket/detail_"
						+ sightId + ".html#from=qunarindex";
				// 景點詳細頁html
				String html = openUrl(detailUrl, "utf-8");
			}
		}
	}

	/**
	 * 訪問url返回url的html代碼
	 */
	public static String openUrl(String currentUrl, String charset) {
		InputStream is = null;
		BufferedReader br = null;
		URL url;
		StringBuffer html = new StringBuffer();
		try {
			url = new URL(currentUrl);
			URLConnection conn = url.openConnection();
			conn.setReadTimeout(5000);
			conn.connect();
			is = conn.getInputStream();
			br = new BufferedReader(new InputStreamReader(is, charset));
			String str;
			while (null != (str = br.readLine())) {
				html.append(str).append("\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
		return html.toString();
	}

}

如果需要門票的詳情,如哪些網站或旅行社在賣門票,各種類別的門票和各種類別門票的價格,需要進入景點詳細頁用正則表達式提取
以上代碼沒有做異常處理和非空判斷,不建議直接使用

解析json用的jar包

commons-beanutils-1.7.0.jar
commons-collections-3.1.jar
commons-lang-2.5.jar
commons-logging.jar
ezmorph-1.0.3.jar
json-lib-2.1-jdk15.jar

源碼和jar已上傳http://download.csdn.net/detail/itjavaer/8386107

發佈了52 篇原創文章 · 獲贊 12 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章