Home  >  Article  >  Database  >  利用EntityFramework获得双色球数据库

利用EntityFramework获得双色球数据库

WBOY
WBOYOriginal
2016-06-07 15:00:011729browse

双色球想必大家都很熟悉了,尽管屡买屡不中,但还是会买。以前就想过利用双色球的走势图得到双色球的数据库,至于得到数据库干什么倒没想过,不过对以往号码有没有重复出现还是挺好奇的。最近写Entity Framework的博客,所以这篇文章的标题里就出现了Entity F

双色球想必大家都很熟悉了,尽管屡买屡不中,但还是会买。以前就想过利用双色球的走势图得到双色球的数据库,至于得到数据库干什么倒没想过,不过对以往号码有没有重复出现还是挺好奇的。最近写Entity Framework的博客,所以这篇文章的标题里就出现了Entity Framework的身影,其实Entity Framework在下面的程序里只占据了很少的一部分。

下面开始介绍我获取数据库的方法。

双色球的走势图网址:http://zx.caipiao.163.com/trend/ssq_basic.html

打开之后,如下图所示,默认显示的是最近30期的:

利用EntityFramework获得双色球数据库

根据期号进行查询,可以得到如下的链接:

http://zx.caipiao.163.com/trend/ssq_basic.html?beginPeriod=2012110&endPeriod=2012139&historyPeriod=2012140&year=

很容易可以发现beginPeriod表示的是开始期号,endPeriod表示的截止期号。有了这两个参数,就可以得到任意期号的数据了。根据上述方法查询,得到网易彩票提供的最早数据是2004009期。

下面分析走势图的html结构。

谷歌浏览器中,按Ctrl+Shift+i 或Firefox中使用Firebug可查看html的结构。

利用EntityFramework获得双色球数据库

下面给出获取

之间内容的代码:

<span>   1:  </span>        <span>/// <summary></summary></span>
<span>   2:  </span>        <span>/// 获取网页的双色球数据</span>
<span>   3:  </span>        <span>/// </span>
<span>   4:  </span>        <span>/// <param name="startQH">开始期号</span>
<span>   5:  </span>        <span>/// <param name="endQH">截止期号</span>
<span>   6:  </span>        <span>/// <returns></returns></span>
<span>   7:  </span>        <span>private</span> <span>string</span> GetOriginData(<span>string</span> startQH, <span>string</span> endQH)
<span>   8:  </span>        {
<span>   9:  </span>            <span>string</span> path = <span>string</span>.Format(<span>"http://zx.caipiao.163.com/trend/ssq_basic.html?beginPeriod={0}&endPeriod={1}"</span>, startQH, endQH);
<span>  10:  </span>            WebRequest wp = WebRequest.Create(path);
<span>  11:  </span>            Stream s = wp.GetResponse().GetResponseStream();
<span>  12:  </span>            StreamReader sr = <span>new</span> StreamReader(s);
<span>  13:  </span>            <span>string</span> content = sr.ReadToEnd();
<span>  14:  </span>            sr.Close();
<span>  15:  </span>            s.Close();
<span>  16:  </span>            <span>int</span> startIndex = content.IndexOf(<span>"<tbody id='\"cpdata\"'>");

  <pre class="brush:php;toolbar:false"><span>  17:  </span>            <span>int</span> endIndex = content.IndexOf(<span>"</span>
");
<span>  18:  </span>            content = content.Substring(startIndex, endIndex - startIndex).Replace(<span>"<tr class='\"bg_doe\"'>", <span>"<tr>").Replace(<span>"<tr>", <span>"<tr>").Replace(<span>"\r\n"</span>, <span>""</span>);

  <pre class="brush:php;toolbar:false"><span>  19:  </span>            <span>return</span> content;
<span>  20:  </span>        }

中的内容就是和了,下面给出解析 和的代码,有注释,就不多解释了。

<span>   1:  </span>        <span>/// <summary></summary></span>
<span>   2:  </span>        <span>/// 循环解析Tr</span>
<span>   3:  </span>        <span>/// </span>
<span>   4:  </span>        <span>/// <param name="wnRepo"></span>
<span>   5:  </span>        <span>/// <param name="content">
<tbody></tbody>之间的内容</span>
<span>   6:  </span>        <span>private</span> <span>void</span> ResolveTr(IRepository<winno> wnRepo, <span>string</span> content)</winno>
<span>   7:  </span>        {
<span>   8:  </span>            <span>string</span> trContent = <span>string</span>.Empty;
<span>   9:  </span>            WinNo wn = <span>null</span>;
<span>  10:  </span>            Regex regex = <span>new</span> Regex(<span>"<tr>");

  <pre class="brush:php;toolbar:false"><span>  11:  </span>            <span>//在<tbody></tbody>之间的内容搜索所有匹配<tr>的项

  <pre class="brush:php;toolbar:false"><span>  12:  </span>            MatchCollection matches = regex.Matches(content);
<span>  13:  </span>            <span>foreach</span> (Match item <span>in</span> matches)
<span>  14:  </span>            {
<span>  15:  </span>                wn = <span>new</span> WinNo();
<span>  16:  </span>                <span>//如果当前匹配项的下一个匹配项的值不为空</span>
<span>  17:  </span>                <span>if</span> (!<span>string</span>.IsNullOrEmpty(item.NextMatch().Value))
<span>  18:  </span>                {
<span>  19:  </span>                    trContent = content.Substring(item.Index, item.NextMatch().Index - item.Index);
<span>  20:  </span>                }
<span>  21:  </span>                <span>//最后一个<tr>的匹配项

  <pre class="brush:php;toolbar:false"><span>  22:  </span>                <span>else</span>
<span>  23:  </span>                {
<span>  24:  </span>                    trContent = content.Substring(item.Index, content.Length - item.Index);
<span>  25:  </span>                }
<span>  26:  </span>                ResolveTd(wn, trContent);
<span>  27:  </span>                wnRepo.Insert(wn);
<span>  28:  </span>            }
<span>  29:  </span>        }
<span>  30:  </span>        <span>/// <summary></summary></span>
<span>  31:  </span>        <span>/// 在一个TR中,解析TD,获取一期的号码</span>
<span>  32:  </span>        <span>/// </span>
<span>  33:  </span>        <span>/// <param name="wn"></span>
<span>  34:  </span>        <span>/// <param name="trContent"></span>
<span>  35:  </span>        <span>private</span> <span>void</span> ResolveTd(WinNo wn, <span>string</span> trContent)
<span>  36:  </span>        {
<span>  37:  </span>            <span>//匹配期号的表达式</span>
<span>  38:  </span>            <span>string</span> patternQiHao = <span>"</span><td align='\"center\"' title='\"开奖日期"</span'>;

  <pre class="brush:php;toolbar:false"><span>  39:  </span>            Regex regex = <span>new</span> Regex(patternQiHao);
<span>  40:  </span>            Match qhMatch = regex.Match(trContent);
<span>  41:  </span>            wn.QiHao = trContent.Substring(qhMatch.Index + 17 + patternQiHao.Length, 7);
<span>  42:  </span>            <span>//匹配蓝球的表达式</span>
<span>  43:  </span>            <span>string</span> patternChartBall02 = <span>"</span><td class='\"chartBall02\"'>";

  <pre class="brush:php;toolbar:false"><span>  44:  </span>            regex = <span>new</span> Regex(patternChartBall02);
<span>  45:  </span>            Match bMatch = regex.Match(trContent);
<span>  46:  </span>            wn.B = Convert.ToInt32(trContent.Substring(bMatch.Index + patternChartBall02.Length, 2));
<span>  47:  </span>            <span>//存放匹配出来的红球号码</span>
<span>  48:  </span>            redBoxList = <span>new</span> Listint>();
<span>  49:  </span>            <span>//匹配红球的表达式</span>
<span>  50:  </span>            <span>string</span> patternChartBall01 = <span>"</span><td class='\"chartBall01\"'>";

  <pre class="brush:php;toolbar:false"><span>  51:  </span>            regex = <span>new</span> Regex(patternChartBall01);
<span>  52:  </span>            MatchCollection rMatches = regex.Matches(trContent);
<span>  53:  </span>            <span>foreach</span> (Match r <span>in</span> rMatches)
<span>  54:  </span>            {
<span>  55:  </span>                redBoxList.Add(Convert.ToInt32(trContent.Substring(r.Index + patternChartBall01.Length, 2)));
<span>  56:  </span>            }
<span>  57:  </span>            <span>//匹配红球的表达式</span>
<span>  58:  </span>            <span>string</span> patternChartBall07 = <span>"</span><td class='\"chartBall07\"'>";

  <pre class="brush:php;toolbar:false"><span>  59:  </span>            regex = <span>new</span> Regex(patternChartBall07);
<span>  60:  </span>            rMatches = regex.Matches(trContent);
<span>  61:  </span>            <span>foreach</span> (Match r <span>in</span> rMatches)
<span>  62:  </span>            {
<span>  63:  </span>                redBoxList.Add(Convert.ToInt32(trContent.Substring(r.Index + patternChartBall07.Length, 2)));
<span>  64:  </span>            }
<span>  65:  </span>            <span>//排序红球号码</span>
<span>  66:  </span>            redBoxList.Sort();
<span>  67:  </span>            <span>//第一个红球号码</span>
<span>  68:  </span>            wn.R1 = redBoxList[0];
<span>  69:  </span>            <span>//第二个红球号码</span>
<span>  70:  </span>            wn.R2 = redBoxList[1];
<span>  71:  </span>            wn.R3 = redBoxList[2];
<span>  72:  </span>            wn.R4 = redBoxList[3];
<span>  73:  </span>            wn.R5 = redBoxList[4];
<span>  74:  </span>            wn.R6 = redBoxList[5];
<span>  75:  </span>        }

下面给出使用到Entity Framework部分的代码:

首先,新建一个WinNo实体,用于表示双色球信息:

<span>   1:  </span>    <span>public</span> <span>class</span> WinNo
<span>   2:  </span>    {
<span>   3:  </span>        <span>/// <summary></summary></span>
<span>   4:  </span>        <span>/// 主键</span>
<span>   5:  </span>        <span>/// </span>
<span>   6:  </span>        <span>public</span> <span>int</span> ID { get; set; }
<span>   7:  </span>        <span>/// <summary></summary></span>
<span>   8:  </span>        <span>/// 期号</span>
<span>   9:  </span>        <span>/// </span>
<span>  10:  </span>        <span>public</span> <span>string</span> QiHao { get; set; }
<span>  11:  </span> 
<span>  12:  </span>        <span>/// <summary></summary></span>
<span>  13:  </span>        <span>/// 第一个红球号码</span>
<span>  14:  </span>        <span>/// </span>
<span>  15:  </span>        <span>public</span> <span>int</span> R1 { get; set; }
<span>  16:  </span>        <span>/// <summary></summary></span>
<span>  17:  </span>        <span>/// 第二个红球号码</span>
<span>  18:  </span>        <span>/// </span>
<span>  19:  </span>        <span>public</span> <span>int</span> R2 { get; set; }
<span>  20:  </span>        <span>/// <summary></summary></span>
<span>  21:  </span>        <span>/// 第三个红球号码</span>
<span>  22:  </span>        <span>/// </span>
<span>  23:  </span>        <span>public</span> <span>int</span> R3 { get; set; }
<span>  24:  </span>        <span>/// <summary></summary></span>
<span>  25:  </span>        <span>/// 第四个红球号码</span>
<span>  26:  </span>        <span>/// </span>
<span>  27:  </span>        <span>public</span> <span>int</span> R4 { get; set; }
<span>  28:  </span>        <span>/// <summary></summary></span>
<span>  29:  </span>        <span>/// 第五个红球号码</span>
<span>  30:  </span>        <span>/// </span>
<span>  31:  </span>        <span>public</span> <span>int</span> R5 { get; set; }
<span>  32:  </span>        <span>/// <summary></summary></span>
<span>  33:  </span>        <span>/// 第六个红球号码</span>
<span>  34:  </span>        <span>/// </span>
<span>  35:  </span>        <span>public</span> <span>int</span> R6 { get; set; }
<span>  36:  </span>        <span>/// <summary></summary></span>
<span>  37:  </span>        <span>/// 篮球号码</span>
<span>  38:  </span>        <span>/// </span>
<span>  39:  </span>        <span>public</span> <span>int</span> B { get; set; }
<span>  40:  </span>    }

其次,使用默认配置即可。

第三,新建一个上下文:SSQContext,代码如下:

<span>   1:  </span>    <span>public</span> <span>class</span> SSQContext : DbContext
<span>   2:  </span>    {
<span>   3:  </span>        <span>public</span> SSQContext()
<span>   4:  </span>        {
<span>   5:  </span>            <span>//Database.SetInitializer(new DropCreateDatabaseAlways<ssqcontext>());</ssqcontext></span>
<span>   6:  </span>            Database.SetInitializer<ssqcontext>(<span>null</span>);</ssqcontext>
<span>   7:  </span>        }
<span>   8:  </span> 
<span>   9:  </span>        <span>public</span> DbSet<winno> WinNos { get; set; }</winno>
<span>  10:  </span> 
<span>  11:  </span>        <span>protected</span> <span>override</span> <span>void</span> OnModelCreating(DbModelBuilder modelBuilder)
<span>  12:  </span>        {
<span>  13:  </span>            modelBuilder.Conventions.Remove<pluralizingtablenameconvention>();</pluralizingtablenameconvention>
<span>  14:  </span>            <span>base</span>.OnModelCreating(modelBuilder);
<span>  15:  </span>        }
<span>  16:  </span>    }

第四,运行程序,结果如下图所示:

利用EntityFramework获得双色球数据库

本程序的源代码下载地址为:http://www.ef-community.com/forum.php?mod=viewthread&tid=44&extra=page%3D1

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn