本文同步发表于我的微信公众号,扫一扫文章底部的二维码或在微信搜索 极客导航 即可关注,每个工作日都有文章更新。
一、概况
我们手里有了2389条双色球数据,除了昨天分析了红球和篮球出现的次数。还能干什么呢?我们脑海中出现了一个清晰而又可笑的商业模式。有没有人想知道这次买的双色球历史上是否中过奖呢?, 我好奇的去网上搜了搜,果然找到了一个网站(china-ssq.com/) 截图如下:
二、实现网站
- 模型
我用的是Django框架来实现这个网站的,首先我根据数据库字段反向生成了模型类。
python manage.py inspectdb > ssq/models.py
复制代码
生成的模型类如下:
class SsqInfo(models.Model):red = models.CharField(max_length=45, blank=True, null=True)blue = models.CharField(max_length=45, blank=True, null=True)date = models.CharField(max_length=45, blank=True, null=True)class Meta:managed = Falsedb_table = 'ssq_info'复制代码
- 模板
我们得快速搞定一个页面,大概长个样子:
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>查询双色球是否中奖</title><style>* {margin: 0;padding: 0;}header {font-weight: bold;}ul {list-style: none outside none;}#reddiv {width: 660px;float: left;}#bluediv {width: 400px;float: left;margin: 0 0 0 60px;}li {display: inline-block;margin: 8px 5px;padding: 5px;width: 30px;height: 30px;font: bold 18px/30px arial;border: 1px #ddd solid;color: #444;border-radius: 31px;text-align: center;background-image: -webkit-radial-gradient(circle at top, rgb(247, 247, 247), rgb(222, 222, 222));background-image: radial-gradient(circle at top, rgb(247, 247, 247), rgb(222, 222, 222));}ul > li:hover {cursor: pointer;background-image: -webkit-radial-gradient(circle at top, rgb(247, 247, 247), rgb(255, 204, 204));background-image: radial-gradient(circle at top, rgb(247, 247, 247), rgb(255, 204, 204));}#reddiv header {color: red;}#bluediv header {color: blue;}.redball {color: #fff;background-image: -webkit-radial-gradient(circle at top, rgb(255, 51, 51), rgb(255, 0, 0));background-image: radial-gradient(circle at top, rgb(255, 51, 51), rgb(255, 0, 0));}.blueball {color: #fff;background-image: -webkit-radial-gradient(circle at top, rgb(0, 85, 204), rgb(0, 0, 225));background-image: radial-gradient(circle at top, rgb(0, 85, 204), rgb(0, 0, 225));}</style>
</head><body>
<div id="reddiv"><header>红球区</header><ul id="redul"><li>01</li><li>02</li><li>03</li><li>04</li><li>05</li><li>06</li><li>07</li><li>08</li><li>09</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li></ul>
</div>
<div id="bluediv"><header>蓝球区</header><ul><li>01</li><li>02</li><li>03</li><li>04</li><li>05</li><li>06</li><li>07</li><li>08</li><li>09</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li></ul>
</div><div><button type="submit" id="btn">查询</button>
</div><div style="width: 1000px" id="infodiv"></div></body>
</html>
复制代码
数据有了,模型类有了,剩下的就是我们最关键的业务逻辑了。
- 业务
首先我们需要知道双色球的中奖规则,抓紧百度一下:
from django.shortcuts import render, HttpResponsefrom .models import SsqInfo
import json
from django.views.decorators.csrf import csrf_exempt# Create your views here.
@csrf_exempt
def index(request):if request.method == 'GET':return render(request, 'index.html')else:# 前端参数reds = request.POST.getlist('red')blue = request.POST.get('blue')# ['11', '22', '33', '07', '06', '18']->[6, 7, 11, 18, 22, 33]reds = sorted([int(i) for i in reds])# [6, 7, 11, 18, 22, 33]->'06,07,11,18,22,33'red = ",".join([str(i).zfill(2) for i in reds])# 返回结果results = []# 先根据红球取出红球全部一样的red_ssqs = SsqInfo.objects.filter(red=red).all()for obj in red_ssqs:if obj.blue == blue:d = {"red": obj.red, "blue": obj.blue, "date": obj.date, 'desc': "6红1蓝 一等奖"}results.append(d)else:d = {"red": obj.red, "blue": obj.blue, "date": obj.date, 'desc': "6红0蓝 二等奖"}results.append(d)# 在取出全部双色球ssqs = SsqInfo.objects.all()for obj in ssqs:s1 = set(sorted(set(([int(i) for i in obj.red.split(",")]))))s2 = set(reds)# 历史s1={8,12,16,19,26,32}# 前端s2 = [6, 7, 16, 18, 22, 33]# s1&s2的交集 {16}length = len(s1 & s2) # 取交集长度# 三等奖if length == 5 and obj.blue == blue:d = {"red": obj.red, "blue": obj.blue, "date": obj.date, 'desc': "5红1蓝 三等奖"}results.append(d)# 四等奖if length == 5 and obj.blue != blue:d = {"red": obj.red, "blue": obj.blue, "date": obj.date, 'desc': "中5红0蓝 四等奖"}results.append(d)if length == 4 and obj.blue == blue:d = {"red": obj.red, "blue": obj.blue, "date": obj.date, 'desc': "中4红1蓝 四等奖"}results.append(d)# 五等奖if length == 4 and obj.blue != blue:d = {"red": obj.red, "blue": obj.blue, "date": obj.date, 'desc': "中4红0蓝 五等奖"}results.append(d)if length == 3 and obj.blue == blue:d = {"red": obj.red, "blue": obj.blue, "date": obj.date, 'desc': "中3红1蓝 五等奖"}results.append(d)# 六等奖if length == 2 and obj.blue == blue:d = {"red": obj.red, "blue": obj.blue, "date": obj.date, 'desc': "中2红1蓝 六等奖"}results.append(d)if length == 1 and obj.blue == blue:d = {"red": obj.red, "blue": obj.blue, "date": obj.date, 'desc': "中2红1蓝 六等奖"}results.append(d)if length == 0 and obj.blue == blue:d = {"red": obj.red, "blue": obj.blue, "date": obj.date, 'desc': "中0红1蓝 六等奖"}results.append(d)return HttpResponse(json.dumps({"data": results}, ensure_ascii=False), content_type='application/json')
复制代码
业务写完,我们前端用ajax调用一下,把返回的数据填在前端页面就基本上完事了。
" class="hljs script"><script>var arrayRed = new Array();var arrayBlue = new Array();$('#reddiv ul>li').click(function () {$(this).toggleClass('redball');var val = $(this).html()if ($.inArray(val, arrayRed) == -1) {arrayRed.push(val)} else {index = arrayRed.indexOf(val)arrayRed.splice(index, 1)}})$('#bluediv ul>li').click(function () {$(this).toggleClass('blueball');var val = $(this).html()if ($.inArray(val, arrayBlue) == -1) {arrayBlue.push(val)} else {index = arrayBlue.indexOf(val)arrayBlue.splice(index, 1)}});$("#btn").click(function (event) {if (arrayRed.length != 6) {alert('请选择6个红球')} else if (arrayBlue.length != 1) {alert('请选择1个蓝球')} else {div = $('#infodiv')$.ajax({type: "POST",data: {red: arrayRed, blue: arrayBlue},dataType: "json",traditional: true,success: function (data) {div.empty()$.each(data.data, function (index, d) {div.append("<div>" + d.red + "-----" + d.blue + "-----" + d.date + "-----" + d.desc +"</div>")})}})}})</script>
复制代码
调通以后,我抓紧试了一下。看看效果咋样?
三、商业模式
来,咱们先像大佬一样,吹一波商业模式。到底能不能用这个每天赚个煎饼果子钱?我觉得是能的,首先如果只想建个网站就有点Low B
了。现在小程序这么发达,完全可以写一个小程序,然后把小程序的二维码沾到大街小巷的彩票站,至少有一些人会好奇,自己选的号在历史上是否中过奖、中了多少次、中了多少钱,就会拿出手机扫一下。
就这个程序,一个共享虚拟主机就能搞定,一年服务器费用几十块钱。我就不相信一年几十块钱撸不回来。当你服务器撑不住的时候,也就是你赚大钱的时候到了。
目前此程序功能还不太完善,你不能只盯着双色球,还有其他的彩种,也是可以搞定的。在服务器上搞一个定时爬取,每天更新数据。当然你可以提供更多特色功能,比如根据姓名、生日、老婆名字生成双色球、vip、svip、ssvip等各种服务,让你享受帝王般的待遇。其他的功能我就不一一跟你们捣鼓了,自己想吧!
四、总结
虽然我们这辈子可能不会中双色球一等奖,但是我们确实可以这些数据干点有意思的事情。
欢迎关注我的公众号,我们一起学习。