[ 本帖最后由 shangjS009 于 2018-5-25 16:42 编辑 ]\n\n[ 本帖最后由 shangjS009 于 2018-5-25 16:00 编辑 ]\n\n[ 本帖最后由 shangjS009 于 2018-5-25 15:50 编辑 ]\n\n[ 本帖最后由 shangjS009 于 2018-5-25 15:49 编辑 ]\n\n[ 本帖最后由 shangjS009 于 2018-5-25 15:44 编辑 ]\n\n[Java] 纯文本查看 复制代码 [ 本帖最后由 shangjS009 于 2018-5-25 15:41 编辑 ]\n\n@RequestMapping(value = "${adminPath}/python/rFeichangzhun")
public class RFeichangzhunController extends BaseController {
private static CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建一个客户端
private static String filename = "d:" + File.separator + File.separator
+ "zhuye.html"; // 定义输出html文件的路径
private static String filenames = "d:" + File.separator + File.separator
+ "xiangxi.html"; // 定义输出html文件的路径
private static String outfile = "d:" + File.separator + File.separator
+ "liudehua.csv"; // 定义输出csv文件的路径
private static boolean bfile = true; // 定义控制输出file的boolean变量
private static boolean bdb = true; // 定义控制输出file的boolean变量
private static ArrayList<String> datalist = new ArrayList<String>(); // 定义Arraylist类集用来保存每一条数据的信息
private static String headtitle = "航班信息,计划起飞,出发地,计划到达,到达地"; // 打印的标题头
private static int countrs = 0; // 计数变量
/**
* 输出html文件
*/
public static void htmltoFile(String htmlString) throws Exception {
// 获得文件输出流
FileOutputStream output = new FileOutputStream(filename);
// 以utf-8编码的形式输出到文件(utf-8是中文编码,ISO-8859-1是英文编码)
output.write(htmlString.getBytes("utf-8"));
if (output != null) {
output.close();
}
}
/**
* 下载页面
*/
public static String downloadPage(String url) throws Exception {
String htmlString = ""; // 定义返回的String变量
HttpGet request = new HttpGet(url); // 请求资源
CloseableHttpResponse response = httpclient.execute(request); // 得到回应
try {
System.out.println(response.getStatusLine()); // 打印状态码
HttpEntity entity = response.getEntity(); // 获得Entity对象
htmlString = EntityUtils.toString(entity); // 将Entity对象转化为字符串
EntityUtils.consume(entity); // 销毁对象
} finally {
response.close();
}
htmltoFile(htmlString); // 调用htmltoFile()方法在制定路径输出html文件
return htmlString;
}
@RequiresPermissions("python:rFeichangzhun:view")
@RequestMapping(value = { "python" })
public String python(RFeichangzhun rFeichangzhun,
HttpServletRequest request, HttpServletResponse response,
Model model) throws Exception {
String didian = request.getParameter("didian");
String riqi = request.getParameter("riqi");
String hangbanhao = request.getParameter("hangbanhao");
String url = "http://www.variflight.com/flight/fnum/" + hangbanhao
+ ".html?AE71649A58c77&fdate=" + riqi;
String url2 = "http://www.variflight.com/schedule/"+didian+"-"+hangbanhao+".html?AE71649A58c77=&fdate="+riqi;
getDouBanList(url, riqi, url2);
return "modules/python/rFeichangzhunList";
}
/**
* 获取非常准航班信息
*
* @throws Exception
*/
public void getDouBanList(String surl, String riqi, String surls)
throws Exception {
String html = RFeichangzhunController.downloadPage(surl); // 通过url下载页面
String html2 = RFeichangzhunController.downloadPages(surls);
html = html.replace("star clearfix", "star_clearfix"); // 用"star_clearfix"替代"star clearfix"
html2 = html2.replace("star clearfix", "star_clearfix");
Document doc = Jsoup.parse(html); // 解析获取Document对象
Document docs = Jsoup.parse(html2);
Element divNode = doc.getElementsByClass("li_box").first(); // 通过getElementsByClass方法获取class为"li_box"的div节点对象
Element divNodes = docs.getElementsByClass("flyProc").first();
Elements liTag = divNode.select("li[style]"); // 通过select选择器选择有class属性的li标签节点,返回Element元素的集合
// Elements liTag1 = divNodes.select("span[class]");
Elements liTag2 = divNodes.select("div[class]");
// System.out.println(liTag2);
String title, jpg;
for (Element liNode : liTag) { // 对于liTag Element集合中的每一个元素liNode
Element dd = liNode.select("div").first(); // 取得liNode的第一个a节点对象
title = dd.getElementsByTag("span").text(); // 使用getElementsByTag方法,通过标签名称取得a标签节点对象,然后取其中的文本元素,即为电影名称
jpg = dd.getElementsByTag("span").html();
String[] strs = title.split(" ");
RFeichangzhun rFeichangzhun = new RFeichangzhun();
rFeichangzhun.setQueryDate(riqi);
String a = (strs[0].toString() + " " + strs[1].toString());
rFeichangzhun.setFlightXinxi(a);
String b = strs[2].toString();
rFeichangzhun.setPlanTime(b);
String c = strs[4].toString();
rFeichangzhun.setChufadi(c);
String d = strs[5].toString();
String e = strs[7].toString();
rFeichangzhun.setDaodadi(e);
String f = strs[9].toString();
rFeichangzhun.setStatus(f);
for (Element liNodes : liTag2) {
Element bb = liNodes.select("div").first();
title = bb.getElementsByTag("span").text();
String[] strs2 = title.split(" ");
String aa = strs2[1].toString();
String cc = strs2[strs2.length - 1].toString();// 8
System.out.println(aa);
System.out.println(cc);
[/mw_shl_code]这个是我自己写着玩的 |