这个抢火车票很给力 啊

抢火车票利器:分享一个抓取火车票转让信息的小程序

从 博客园-首页原创精华区  作者:guozili@163.com
有超过 100 人喜欢此条目

 

这个抢火车票很给力 啊 - Hello World - 火柴天堂 代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Net;
using  System.ComponentModel;
using  System.Threading;
using  System.Text.RegularExpressions;
using  System.Diagnostics;

namespace  HuoChePiao
{
    
class  Program
    {
        
private   static  HashSet < string >  results;
        
private   static  WebClient wc;
        
private   static  List < Site >  sites;
        
private   static   int  index;
        
private   static   bool  isFirstRound;
        
        
static   void  Main( string [] args)
        {
            sites 
=   new  List < Site > ();
            
            sites.Add(
new  Site()
            {
                Name 
=   " 火车票网 " ,
                Url 
=   " http://www.huochepiao.com/City/SearchCheCi.asp?leixing=%D7%AA%C8%C3&zhuti=t189&psearch=%C7%F3%B9%BA%2F%D7%AA%C8%C3%BC%EC%CB%F7 " ,
                RegexPattern 
=   @" · <A href=(.*?) target=_blank>(.*?)</a> " ,
                Encoding 
=  Encoding.Default
            });
            
            sites.Add(
new  Site()
            {
                Name 
=   " 百姓网 " ,
                Url 
=   " http://beijing.baixing.com/huochepiao/?%E8%BD%A6%E6%AC%A1=T189 " ,
                RegexPattern 
=   @" "" ><a href=""/(.*?)"">(.*?)</a></td> " ,
                Encoding 
=  Encoding.UTF8,
                Domain 
=   " http://beijing.baixing.com/ " ,
                Keys 
=   new   string [] {  " "  }
            });

            sites.Add(
new  Site()
            {
                Name 
=   " 百姓网 " ,
                Url 
=   " http://beijing.baixing.com/huochepiao/?%E8%BD%A6%E6%AC%A1=T5 " ,
                RegexPattern 
=   @" "" ><a href=""/(.*?)"">(.*?)</a></td> " ,
                Encoding 
=  Encoding.UTF8,
                Domain 
=   " http://beijing.baixing.com/ " ,
                Keys 
=   new   string [] {  " "  }
            });
   
            sites.Add(
new  Site()
            {
                Name 
=   " 酷讯网 " ,
                Url 
=   " http://huoche.kuxun.cn/zhuanrang-checi-beijing-T189.html?type=1 " ,
                RegexPattern 
=   @" <span class=""col_11 left""><a target=""_blank"" href=""(.*)"">(.*)</a> " ,
                Encoding 
=  Encoding.UTF8
            });

            sites.Add(
new  Site()
            {
                Name 
=   " 酷讯网 " ,
                Url 
=   " http://huoche.kuxun.cn/zhuanrang-checi-beijing-T5.html?type=1 " ,
                RegexPattern 
=   @" <span class=""col_11 left""><a target=""_blank"" href=""(.*)"">(.*)</a> " ,
                Encoding 
=  Encoding.UTF8
            });

            sites.Add(
new  Site()
            {
                Name 
=   " 酷讯网 " ,
                Url 
=   " http://huoche.kuxun.cn/zhuanrang-checi-beijing-k185.html?type=1 " ,
                RegexPattern 
=   @" <span class=""col_11 left""><a target=""_blank"" href=""(.*)"">(.*)</a> " ,
                Encoding 
=  Encoding.UTF8
            });

            sites.Add(
new  Site()
            {
                Name 
=   " 清华网 " ,
                Url 
=   " http://www.newsmth.net/bbsbfind.php?q=1&board=Ticket&title=T189&title3=%C7%F3&dt=1 " ,
                RegexPattern 
=   @" <a href=""(bbscon.php.*)"">(.*)</a> " ,
                Encoding 
=  Encoding.Default,
                Domain 
=   " http://www.newsmth.net/ "
            });

            sites.Add(
new  Site()
            {
                Name 
=   " 清华网 " ,
                Url 
=   " http://www.newsmth.net/bbsbfind.php?q=1&board=Ticket&title=K185&title3=%C7%F3&dt=1 " ,
                RegexPattern 
=   @" <a href=""(bbscon.php.*)"">(.*)</a> " ,
                Encoding 
=  Encoding.Default,
                Domain 
=   " http://www.newsmth.net/ "
            });
            
            results 
=   new  HashSet < string > ();

            isFirstRound 
=   true ;

            wc 
=   new  WebClient();
            wc.Encoding 
=  sites[index].Encoding;

            wc.DownloadStringCompleted 
+=   new  DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            wc.DownloadStringAsync(
new  Uri(sites[index].Url));

            
int  order;         
            
while  ( int .TryParse(Console.ReadLine(),  out  order))
            {
                OpenLink(results.ElementAt(order 
-   1 ));
                Console.ForegroundColor 
=  ConsoleColor.Green;
            }
        }

        
static   void  wc_DownloadStringCompleted( object  sender, DownloadStringCompletedEventArgs e)
        {
            
int  i  =   0 ;
            
            MatchCollection mc 
=  Regex.Matches(e.Result, sites[index].RegexPattern);
            
foreach  (Match m  in  mc)
            {
                
if  (m.Success)
                {
                    
string  result  =  sites[index].Domain  +  m.Groups[ 1 ].Value;
                    
string  content  =  m.Groups[ 2 ].Value;
                    
if  ( ! results.Contains(result))
                    {
                        
bool  isContainKey  =   true ;
                        
if  (sites[index].Keys  !=   null )
                        {
                            
foreach  ( string  key  in  sites[index].Keys)
                            {
                                
if  ( ! content.Contains(key))
                                {
                                    isContainKey 
=   false ;
                                    
break ;
                                }
                            }
                        }

                        
if  ( ! isContainKey)
                            
continue ;

                        results.Add(result);

                        Console.ForegroundColor 
=  isFirstRound  ?  ConsoleColor.Gray : ConsoleColor.White;

                        Console.WriteLine(
" {0} <{1}>  {2}.  {3} " , DateTime.Now.ToShortTimeString(), sites[index].Name, results.Count, m.Groups[ 2 ].Value);

                        
if  ( ! isFirstRound)
                        {
                            OpenLink(result);
                        }               
                    }
                }

                
if  (i ++   >  sites[index].Numbers  -   2 )
                    
break ;
            }

            Thread.Sleep(
1000 );

            index 
=  index  +   1 ;
            
if  (index  ==  sites.Count)
            {
                index 
=   0 ;

                
if  (isFirstRound)
                    isFirstRound 
=   false ;
            }

            wc.Encoding 
=  sites[index].Encoding;
            wc.DownloadStringAsync(
new  Uri(sites[index].Url));
        }

        
static   void  OpenLink( string  url)
        {
            Process.Start(url, 
" _blank " );
        }
    }

    
public   class  Site
    {
        
// 站点名称
         public   string  Name {  get set ; }
        
// 站点的网址
         public   string  Url {  get set ; }
        
// 正则表达式
         public   string  RegexPattern {  get set ; }
        
// 编码
         public  Encoding Encoding {  get set ; }
        
// 网站名称
         public   string  Domain {  get set ; }
        
// 包含关键字
         public   string [] Keys {  get set ; }
        
// 取前面多少条
         public   int  Numbers {  get set ; }

        
public  Site()
        {
            
this .Numbers  =   3 ;
        }
    }
}

 

 

每年都用这个抢到票了,今天也不例外,分享给大家:

1. 里面的规则你可以自己添加,我已经写好了很多,你把网址里德车次改下就行了

2. 初始化会抓取最新的信息,但是不弹出网页,你可以在控制台输入id号,弹出对应id号的网址

3. 后面会自动循环捕获每个站点的新信息,并弹出网页

 

快就一个字,希望大家都能买到火车票,安心回家

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值