比如想获得去哪网北京所有门票的信息,地址是http://piao.qunar.com/ticket/list.htm?keyword=%E5%8C%97%E4%BA%AC®ion=&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®ion=&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