用groovy写抓票程序

用groovy写抓票程序 - 疯狂的菠菜 - ITeye技术网站

年底了能买到火车票是非常幸运的事儿, 比如我同事, 通过电话就订到了车票, 而我死活都没打进那个电话.



于是用groovy写了个程序, 用来抓取火车票信息, 网上相关的程序还不少, 我只是用groovy来练练手而已, 本来可以完善一下, 像这个(http://www.cnblogs.com/guozili/archive/2011/01/19/1939157.html)可以从多个网站抓取, 像这个(http://www.notedit.com/2010/11/%E6%8A%A2%E7%81%AB%E8%BD%A6%E7%A5%A8%E7%9A%84%E7%A8%8B%E5%BA%8F/)可以定时抓取, 本来我想通过定时抓取发消息的, 后来搞到了票, 就这样吧.




Java代码  收藏代码
  1. class GetTicket {  
  2.     final static String host = "http://hz.58.com/huochepiao/?StartStation=%25u676D%25u5DDE&EndStation=%25u5B9C%25u660C"  
  3.     // 最早发车时间  
  4.     final static int earliest = 120  
  5.     // 已经确认无票的过滤掉  
  6.     final static List filterList = [  
  7.     "http://hz.58.com/huochepiao/4538967059457x.shtml",   
  8.     "http://hz.58.com/huochepiao/4536633437697x.shtml"  
  9.     ]  
  10.       
  11.     def void get() {  
  12.         def htmlSource =  new Http().get(host).source.toString()  
  13.         int i = 0  
  14.         LinkedList<Entry> list = [] as LinkedList<Entry>;  
  15.         htmlSource.eachLine{  
  16.             if (i > 0 && i <= 4) {  
  17.                 switch(i) {  
  18.                     case 1:  
  19.                         list[list.size()-1].location = it.trim()  
  20.                         break;  
  21.                     case 2:  
  22.                         list[list.size()-1].number = it.trim()  
  23.                         break;  
  24.                     case 3:  
  25.                         list[list.size()-1].type = it.trim()  
  26.                         break;  
  27.                     case 4:  
  28.                         def matcher = it.trim() =~ /(.+)<\/a>/  
  29.                         def pair = matcher[0][1].split(" ")  
  30.                         pair[1] = pair[1].replaceAll(/月|日/, "")  
  31.                         list[list.size()-1].count = pair[0]  
  32.                         list[list.size()-1].date = pair[1]  
  33.                         if (Integer.valueOf(pair[1]) < earliest) {  
  34.                             list.removeLast()  
  35.                         }  
  36.                         break;  
  37.                 }  
  38.                 i++  
  39.                 return;  
  40.             }else {  
  41.                 i = 0;  
  42.             }  
  43.               
  44.             if (it ==~ /^ +<a href="http:\/\/hz\.58\.com\/huochepiao.+/){  
  45.                 def matcher = it =~ /"(http:\/\/hz\.58\.com\/huochepiao.+?)"/  
  46.                 def url = matcher[0][1].trim()  
  47.                 if (filterList.contains(url)) {  
  48.                     return;  
  49.                 }  
  50.                 Entry entry = [:] as Entry  
  51.                 entry.url = url  
  52.                 list << entry  
  53.                 i++  
  54.                   
  55.                 // 临近站信息  
  56.                 matcher = it =~ /.+>(.+)$/  
  57.                 if (matcher.matches()) {  
  58.                     entry.location = matcher[0][1].trim()  
  59.                     i++  
  60.                 }  
  61.             }  
  62.         }  
  63.           
  64.         list = list.sort()  
  65.           
  66.         list.each{ println "${it.date}\t${it.count}\t ${it.type}\t ${it.number}\t ${it.location}\t ${it.url}" }  
  67.     }  
  68. }  
  69. class Entry implements Comparable{  
  70.     def url  
  71.     def location  
  72.     def number  
  73.     def type  
  74.     def count  
  75.     def date  
  76.     int compareTo( def other) {  
  77.         return Integer.valueOf(other.date) - Integer.valueOf(date)  
  78.     }  
  79.       
  80.     @Override  
  81.     public String toString() {  
  82.         return ToStringBuilder.reflectionToString(this);  
  83.     }  
  84. }  




注: 解析html用到了com.googlecode.groovyhttp.Http 这个lib(http://code.google.com/p/groovy-http/)



输出结果:


引用

131 1张 硬卧 K529 杭州 - 恩施 http://hz.58.com/huochepiao/4555300451203x.shtml

129 1张 硬卧 K529 杭州 - 荆门 http://hz.58.com/huochepiao/4547173468803x.shtml

128 1张 K529 杭州 - 恩施 http://hz.58.com/huochepiao/4547524308355x.shtml

124 1张 硬座 K253 杭州 - 宜昌 http://hz.58.com/huochepiao/4557214747137x.shtml

123 1张 硬卧 K253 杭州南 - 宜昌 http://hz.58.com/huochepiao/4557440945411x.shtml

123 1张 站票 K529 杭州 - 宜昌 http://hz.58.com/huochepiao/4532977245187x.shtml

122 1张 硬座 K253 杭州南 - 宜昌 http://hz.58.com/huochepiao/4557377085571x.shtml

122 1张 硬座 K253 杭州南 - 宜昌 http://hz.58.com/huochepiao/4544944871170x.shtml

原文地址:https://www.cnblogs.com/lexus/p/2649566.html