1. 程式人生 > >2017年雙色球中獎號碼

2017年雙色球中獎號碼

一個 for lxml 蜘蛛 網絡機器人 分享 turn red main

1.網絡爬蟲與雙色球

網絡爬蟲,又稱網頁蜘蛛、網絡機器人。隨著計算機技術的高速發展,互聯網中的信息量越來越大,搜索引擎應運而生。傳統的搜索引擎會有返回結果不精確等局限性。為了解決傳統搜索引擎的局限性,專用型網絡爬蟲在互聯網中越來越常見。同時,專用型網絡爬蟲具有專用性,可以根據制定的規則和特征,最後只體現和篩選出有用的信息。

在國內,唯一能合法暴富的方法似乎只有彩票中獎了。雖然人人都知道中獎的概率很低,但希望總是存在的。中獎的號碼雖然不能直接推算出來,但根據概率計算將中獎的稍微調大點還是可能的,在進行概率計算前要做的就是收集數據,好在中國福利彩票並不禁止收集數據進行概率計算。

2.

利用python在網頁收集信息

本次實驗主要是爬取2017年雙色球中獎號碼以及統計中獎結果。

import re
from bs4 import BeautifulSoup
import ur11ib2
from mylog import MyLog as mylog


class DoubleColorBallItem(object):
    date=None #開獎日期
    order=None #當年的順序
    red1=None #第一個紅球的號碼
    red2=None #第二個紅球的號碼
    red3=None #第三個紅球的號碼
    red4=None #
第四個紅球的號碼 red5=None #第五個紅球的號碼 red6=None #第六個紅球的號碼 blue=None #籃球號碼 mony=None #彩池金額 firstPrize=None #一等獎中獎人數 secondPrize=None #二等獎中獎人數 class GetDoubleColorBallNumber(object): ‘‘‘這個類用於獲取雙色球中獎號碼,返回一個txt文件 ‘‘‘ def _init_(self): self.urls=[] self.log=mylog() self.getUrls() self.items
=self.spider(self.urls) self.pipelines(self.items) def getUrls(self): ‘‘‘獲取數據來源網頁 ‘‘‘ URL=rhttp://kaijiang.zhcw.com/zhcw/html/ssq/list_1.html htmlContent=self.getResponseContent(URL) soup=BeautifulSoup(htmlContent,lxml) tag=soup.find_all(re.compile(P))[-1] pages=tag.strong.get_text() for i in xrang(1,int(pages)+1): url=rhttp://kaijiang.zhcw.com/zhcw/html/ssq/list_+str(i) +.html self.urls.append(url) self.log.info(u添加URL:%S到URLS\r\n%url) def getResponseContent(self,url): ‘‘‘這裏單獨使用一個函數返回頁面返回值,是為了後期方便地加入proxy和headers等 ‘‘‘ try: respone=urllib2.urlopen(url.encode(utf8)) except: self.log.error(upython返回URL:%s數據失敗 \r\n%url) else: self.log.info(upython返回URL:%s數據成功 \r\n%url) return response.read() def spider(self,urls): ‘‘‘這個函數的作用是從獲取的數據中過濾得到中獎信息 ‘‘‘ items=[] for url in urls: htmlContent=self.getResponseContent(url) soup=BeautifulSoup(htmlContent,lxml) tags=soup.find_all(tr,attrs={}) for tag in tags: if tag.find(em): item=DoubleColorBallItem() TagTd=tag.find_all(td) item.date=TagTd[0].get_text() item.order=TagTd[1].get_text() TagEm=TagTd[2].find_all(em) item.red1=tagEm[0].get_text() item.red2=tagEm[1].get_text() item.red3=tagEm[2].get_text() item.red4=tagEm[3].get_text() item.red5=tagEm[4].get_text() item.red6=tagEm[5].get_text() item.blue=tagEm[6].get_text() item.money=tagTd[3].find(strong).get_text() item.firstPrize=tagTd[4].find(strong).get_text() item.secondPrize=tagTd[5].find(strong).get_text() items.append(item) self.log.info(u獲取日期為:%s的數據成功%(item.date)) return items def pipelines(self,items): fileName=u雙色球.txt.encode(GBK) with open(fileName,w) as fp: for item in items: fp.write(%s %s \t %s %s %s %s %s %s %s \t %s \t %s %s \n %(item.date,item.order,item.red1,item.red2,item.red3,item.red4,item.red5,item.red6,item.blue,item.money,item.firstPrize,item.secondPrize)) self.log.info(u將日期為:%s的數據存入"%s"... %(item.date,fileName.decode(GBK))) if _name_==_main_: GDCBN=GetDoubleColorBallNumber()

爬取結果如圖所示(截取部分截圖):

技術分享

技術分享

圖2.1 2017年每期開獎信息

2017年中獎號碼如圖所示(截取部分圖):

技術分享

圖2.2 2017年每期開獎號碼

將中獎號碼保存至qiu.txt,分析統計結果如圖所示:

import jieba   #處理中文需要的庫
fo=open(qiu2.txt,r)#把qiu.txt的內容讀出到of裏
numbers=fo.read()#把of的內容給numbers
#print(numbers)
numberss=list(jieba.cut(numbers))#精準模式來分詞
exp={ ,\n} #把不要統計的詞放在一個集合裏  
dict={} #建立空字典來存需要統計的詞頻         
keys=set(numbers)-exp
#在文的集合裏(詞頻不重復、無序),排除我們不要的詞,即exp裏面的詞
for i in keys: #對一個字典的鍵值(唯一),進行統計
    dict[i]=numbers.count(i)#統計的內容詞頻來自numbers裏(裏面的詞可能會重復)
    count=list(dict.items())
#因為字典式不能排序的,所以要變成可以排序的列表。具體是對字典裏的什麽內容排序呢?就是對字典裏的每一對值排序,怎麽才能對一對排序呢?字典的items()就是輸出一對值得函數。
count.sort(key=lambda x:x[1],reverse=True) #lambda()是個定義函數匿名的
for i in range(33): #打印統計數字的出現次數
    print(count[i])
fo.close()

技術分享

圖2.3 統計2017年中獎號碼

用工具做成詞雲的結果為:

技術分享

圖2.4 2017年中獎號碼詞雲

3.結論

由圖可以看出,2017呢喃雙色球中獎的號碼中,號碼21、15和14和23出現的比較多一些,可以適當考慮這幾個號碼。

2017年雙色球中獎號碼