zoukankan      html  css  js  c++  java
  • 抢火车票利器:分享一个抓取火车票转让信息的小程序

    代码
    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 { getset; }
            
    //站点的网址
            public string Url { getset; }
            
    //正则表达式
            public string RegexPattern { getset; }
            
    //编码
            public Encoding Encoding { getset; }
            
    //网站名称
            public string Domain { getset; }
            
    //包含关键字
            public string[] Keys { getset; }
            
    //取前面多少条
            public int Numbers { getset; }

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

     

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

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

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

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

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

  • 相关阅读:
    浅谈localStorage和sessionStorage的相关用法
    v-for中:key的作用总结
    textarea的placeholder无效问题解决
    6月10日
    6月9日
    6月8日
    6月7日
    6月6日
    10月5日
    6月4日
  • 原文地址:https://www.cnblogs.com/guozili/p/1939157.html
Copyright © 2011-2022 走看看