博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用groovy写抓票程序
阅读量:7236 次
发布时间:2019-06-29

本文共 4862 字,大约阅读时间需要 16 分钟。

博客分类:
年底了能买到火车票是非常幸运的事儿, 比如我同事, 通过电话就订到了车票, 而我死活都没打进那个电话.
于是用groovy写了个程序, 用来抓取火车票信息, 网上相关的程序还不少, 我只是用groovy来练练手而已, 本来可以完善一下, 像这个( )可以从多个网站抓取, 像这个( )可以定时抓取, 本来我想通过定时抓取发消息的, 后来搞到了票, 就这样吧.
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. }  
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
list = [] as LinkedList
; 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 ==~ /^ +
(.+)$/ 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); }}

注: 解析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

转载地址:http://zvofm.baihongyu.com/

你可能感兴趣的文章
[sqlserver]sqlserver2005设置允许远程连接
查看>>
对ASP.NET网站高性能和多并发的设计的讨论
查看>>
组策略之账户安全设置
查看>>
[Unity3d]打包Assetbundle并加载
查看>>
使用我们的DataProvider
查看>>
抓信插件开发遇到网页的CSS不起作用
查看>>
体验microsoft Security Essentials(微软免费杀毒软件)
查看>>
解决Ubuntu中更改MySQL默认编码报错
查看>>
VLAN概念的面试题及解答_路由交换
查看>>
mysql忧化参数
查看>>
深入浅出单实例Singleton设计模式
查看>>
Windows Phone 实用开发技巧(12):让你的Windows Phone应用变得更Metro
查看>>
极速理解设计模式系列:6.适配器模式(Adapter Pattern)
查看>>
Swing与Servlet通信简单示例
查看>>
【一天一个shell命令】文本操作系列-chmod
查看>>
Cisco 3550-SMI IOS升级过程分享
查看>>
Silverlight实用窍门系列:61.Silverlight中的Trigger触发器,自定义翻页触发器
查看>>
JSF Spring Hibernate 整合:JSH1
查看>>
【移动开发】Android中不用图片资源也能做出好看的界面
查看>>
第二章 深入探讨控制反转(Ioc)和依赖注入(DI)之二
查看>>