Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了用groovy写抓票程序大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
年底了能买到火车票是非常幸运的事儿,比如我同事,通过电话就订到了车票,而我死活都没打进那个电话.

于是用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/)可以定时抓取,本来我想通过定时抓取发消息的,后来搞到了票,就这样吧.

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);
    }
}


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

大佬总结

以上是大佬教程为你收集整理的用groovy写抓票程序全部内容,希望文章能够帮你解决用groovy写抓票程序所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。