利用EntityFramework获得双色球数据库
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了利用EntityFramework获得双色球数据库,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7490字,纯文字阅读大概需要11分钟。
内容图文
![利用EntityFramework获得双色球数据库](/upload/InfoBanner/zyjiaocheng/566/44d63dfaccb748daad60036853982eee.jpg)
双色球想必大家都很熟悉了,尽管屡买屡不中,但还是会买。以前就想过利用双色球的走势图得到双色球的数据库,至于得到数据库干什么倒没想过,不过对以往号码有没有重复出现还是挺好奇的。最近写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(IRepositorywnRepo, 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:
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:
9: public DbSetWinNos { get; set; }
10:
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
内容总结
以上是互联网集市为您收集整理的利用EntityFramework获得双色球数据库全部内容,希望文章能够帮你解决利用EntityFramework获得双色球数据库所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。