热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

利用EntityFramework获得双色球数据库-mysql教程

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

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

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

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

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

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

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

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的结构。

下面给出获取之间内容的代码:

   1:          /// 
   2:          /// 获取网页的双色球数据
   3:          /// 
   4:          /// 开始期号
   5:          /// 截止期号
   6:          /// 
   7:          private string GetOriginData(string startQH, string endQH)
   8:          {
   9:              string path = string.Format("http://zx.caipiao.163.com/trend/ssq_basic.html?beginPeriod={0}&endPeriod={1}", startQH, endQH);
  10:              WebRequest wp = WebRequest.Create(path);
  11:              Stream s = wp.GetResponse().GetResponseStream();
  12:              StreamReader sr = new StreamReader(s);
  13:              string cOntent= sr.ReadToEnd();
  14:              sr.Close();
  15:              s.Close();
  16:              int startIndex = content.IndexOf("");
  17:              int endIndex = content.IndexOf("");
  18:              cOntent= content.Substring(startIndex, endIndex - startIndex).Replace("", "").Replace("", "").Replace("\r\n", "");
  19:              return content;
  20:          }

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

   1:          /// 
   2:          /// 循环解析Tr
   3:          /// 
   4:          /// 
   5:          /// 之间的内容
   6:          private void ResolveTr(IRepository wnRepo, string content)
   7:          {
   8:              string trCOntent= string.Empty;
   9:              WinNo wn = null;
  10:              Regex regex = new Regex("");
  11:              //在之间的内容搜索所有匹配的项
  12:              MatchCollection matches = regex.Matches(content);
  13:              foreach (Match item in matches)
  14:              {
  15:                  wn = new WinNo();
  16:                  //如果当前匹配项的下一个匹配项的值不为空
  17:                  if (!string.IsNullOrEmpty(item.NextMatch().Value))
  18:                  {
  19:                      trCOntent= content.Substring(item.Index, item.NextMatch().Index - item.Index);
  20:                  }
  21:                  //最后一个的匹配项
  22:                  else
  23:                  {
  24:                      trCOntent= content.Substring(item.Index, content.Length - item.Index);
  25:                  }
  26:                  ResolveTd(wn, trContent);
  27:                  wnRepo.Insert(wn);
  28:              }
  29:          }
  30:          /// 
  31:          /// 在一个TR中,解析TD,获取一期的号码
  32:          /// 
  33:          /// 
  34:          /// 
  35:          private void ResolveTd(WinNo wn, string trContent)
  36:          {
  37:              //匹配期号的表达式
  38:              string patternQiHao = ";
  39:              Regex regex = new Regex(patternQiHao);
  40:              Match qhMatch = regex.Match(trContent);
  41:              wn.QiHao = trContent.Substring(qhMatch.Index + 17 + patternQiHao.Length, 7);
  42:              //匹配蓝球的表达式
  43:              string patternChartBall02 = "";
  44:              regex = new Regex(patternChartBall02);
  45:              Match bMatch = regex.Match(trContent);
  46:              wn.B = Convert.ToInt32(trContent.Substring(bMatch.Index + patternChartBall02.Length, 2));
  47:              //存放匹配出来的红球号码
  48:              redBoxList = new List<int>();
  49:              //匹配红球的表达式
  50:              string patternChartBall01 = "";
  51:              regex = new Regex(patternChartBall01);
  52:              MatchCollection rMatches = regex.Matches(trContent);
  53:              foreach (Match r in rMatches)
  54:              {
  55:                  redBoxList.Add(Convert.ToInt32(trContent.Substring(r.Index + patternChartBall01.Length, 2)));
  56:              }
  57:              //匹配红球的表达式
  58:              string patternChartBall07 = "";
  59:              regex = new Regex(patternChartBall07);
  60:              rMatches = regex.Matches(trContent);
  61:              foreach (Match r in rMatches)
  62:              {
  63:                  redBoxList.Add(Convert.ToInt32(trContent.Substring(r.Index + patternChartBall07.Length, 2)));
  64:              }
  65:              //排序红球号码
  66:              redBoxList.Sort();
  67:              //第一个红球号码
  68:              wn.R1 = redBoxList[0];
  69:              //第二个红球号码
  70:              wn.R2 = redBoxList[1];
  71:              wn.R3 = redBoxList[2];
  72:              wn.R4 = redBoxList[3];
  73:              wn.R5 = redBoxList[4];
  74:              wn.R6 = redBoxList[5];
  75:          }

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

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

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

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

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

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

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

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

推荐阅读
author-avatar
午夜呻吟的诱惑
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有