大佬教程收集整理的这篇文章主要介绍了用groovy写抓票程序,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
class GetTicket { final static String host = "http://hz.58.com/huochepiao/?StartStation=%25u676D%25u5DDE&EndStation=%25u5B9C%25u660C" // 最早发车时间 final static int earliest = 120 // 已经确认无票的过滤掉 final static List filterList = [ "http://hz.58.com/huochepiao/4538967059457x.shtml","http://hz.58.com/huochepiao/4536633437697x.shtml" ] def void get() { def htmlsource = new http().get(host).source.toString() int i = 0 LinkedList<Entry> list = [] as LinkedList<Entry>; htmlsource.eachLine{ if (i > 0 && i <= 4) { switch(i) { case 1: list[list.size()-1].LOCATIOn = it.trim() break; case 2: list[list.size()-1].number = it.trim() break; case 3: list[list.size()-1].type = it.trim() break; case 4: def matcher = it.trim() =~ /(.+)<\/a>/ def pair = matcher[0][1].split(" ") pair[1] = pair[1].replaceAll(/月|日/,"") list[list.size()-1].count = pair[0] list[list.size()-1].date = pair[1] if (Integer.valueOf(pair[1]) < earliest) { list.removeLast() } break; } i++ return; }else { i = 0; } if (it ==~ /^ +<a href="http:\/\/hz\.58\.com\/huochepiao.+/){ def matcher = it =~ /"(http:\/\/hz\.58\.com\/huochepiao.+?)"/ def url = matcher[0][1].trim() if (filterList.contains(url)) { return; } Entry entry = [:] as Entry entry.url = url list << entry i++ // 临近站信息 matcher = it =~ /.+>(.+)$/ if (matcher.matches()) { entry.LOCATIOn = matcher[0][1].trim() i++ } } } list = list.sort() list.each{ println "${it.datE}\t${it.count}\t ${it.typE}\t ${it.number}\t ${it.LOCATIOn}\t ${it.url}" } } } class Entry implements Comparable{ def url def LOCATIOn def number def type def count def date int compareTo( def other) { return Integer.valueOf(other.datE) - Integer.valueOf(datE) } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } }
以上是大佬教程为你收集整理的用groovy写抓票程序全部内容,希望文章能够帮你解决用groovy写抓票程序所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。