Python數(shù)據(jù)分析之獲取雙色球歷史信息的方法示例
本文實(shí)例講述了Python數(shù)據(jù)分析之獲取雙色球歷史信息的方法。分享給大家供大家參考,具體如下:
每個人都有一顆中雙色球大獎的心,對于技術(shù)人員來說,通過技術(shù)分析,可以增加中獎幾率,現(xiàn)使用python語言收集歷史雙色球中獎信息,之后進(jìn)行預(yù)測分析。
說明:采用2016年5月15日獲取的雙色球數(shù)據(jù)為基礎(chǔ)進(jìn)行分析,總抽獎數(shù)1940次。
初級代碼,有些內(nèi)容比較繁瑣,有更好的代碼,大家可以分享。
#!/usr/bin/python # -*- coding:UTF-8 -*- #coding:utf-8 #author:levycui #date:20160513 #Description:雙色球信息收集 import urllib2 from bs4 import BeautifulSoup #采用BeautifulSoup import os import re #偽裝成瀏覽器登陸,獲取網(wǎng)頁源代碼 def getPage(href): headers = { 'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6' } req = urllib2.Request( url = href , headers = headers ) try: post = urllib2.urlopen(req) except urllib2.HTTPError,e: print e.code print e.reason return post.read() #初始化url 雙色球首頁 url = 'http://kaijiang.zhcw.com/zhcw/html/ssq/list_1.html' #=============================================================================== #獲取url總頁數(shù) def getPageNum(url): num =0 page = getPage(url) soup = BeautifulSoup(page) strong = soup.find('td',colspan='7') # print strong if strong: result = strong.get_text().split(' ') # print result list_num = re.findall("[0-9]{1}",result[1]) # print list_num for i in range(len(list_num)): num = num*10 + int(list_num[i]) return num else: return 0 #=============================================================================== #獲取每頁雙色球的信息 def getText(url): for list_num in range(1,getPageNum(url)): #從第一頁到第getPageNum(url)頁 print list_num #打印下頁碼 #調(diào)用新url鏈接 # for listnum in len(list_num): page = BeautifulSoup(getPage(href)) em_list = page.find_all('em') #匹配em內(nèi)容 div_list = page.find_all('td',{'align':'center'}) #匹配 <td align=center>這樣的內(nèi)容 #初始化n n = 0 #將雙色球數(shù)字信息寫入num.txt文件 fp = open("num.txt" ,"w") for div in em_list: emnum1 = div.get_text() # print emnum1 text = div.get_text() text = text.encode('utf-8') #print title n=n+1 if n==7: text = text + "\n" n=0 else: text = text + "," fp.write(str(text)) fp.close() #將日期信息寫入date.txt文件 fp = open("date.txt" ,"w") for div in div_list: text = div.get_text().strip('') # print text list_num = re.findall('\d{4}-\d{2}-\d{2}',text) list_num = str(list_num[::1]) list_num = list_num[3:13] if len(list_num) == 0: continue elif len(list_num) > 1: fp.write(str(list_num)+'\n') fp.close() #將num.txt和date.txt文件進(jìn)行整合寫入hun.txt文件中 #格式如下: #('2016-05-03', '09,12,24,28,29,30,02') #('2016-05-01', '06,08,13,14,22,27,10') #('2016-04-28', '03,08,13,14,15,30,04') # fp01 = open("date.txt","r") a=[] for line01 in fp01: a.append(line01.strip('\n')) # print a fp01.close() fp02 = open("num.txt","r") b=[] for line02 in fp02: b.append(line02.strip('\n')) # print b fp02.close() fp = open("hun.txt" ,"a") for cc in zip(a,b): #使用zip方法合并 print cc fp.write(str(cc) + '\n') fp.close() #=============================================================================== if __name__=="__main__": pageNum = getPageNum(url) print pageNum getpagetext = getText(url) print getpagetext
數(shù)據(jù)樣例:
('2015-03-03', '09,11,16,18,23,24,10') ('2015-03-01', '08,09,10,13,29,30,01') ('2015-02-26', '04,07,10,16,23,25,10')
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
- Python運(yùn)用于數(shù)據(jù)分析的簡單教程
- Python數(shù)據(jù)分析matplotlib設(shè)置多個子圖的間距方法
- 分享一下Python數(shù)據(jù)分析常用的8款工具
- python數(shù)據(jù)分析數(shù)據(jù)標(biāo)準(zhǔn)化及離散化詳解
- Python數(shù)據(jù)分析之雙色球基于線性回歸算法預(yù)測下期中獎結(jié)果示例
- Python數(shù)據(jù)分析中Groupby用法之通過字典或Series進(jìn)行分組的實(shí)例
- Python數(shù)據(jù)分析:手把手教你用Pandas生成可視化圖表的教程
- PowerBI和Python關(guān)于數(shù)據(jù)分析的對比
- Python數(shù)據(jù)分析庫pandas基本操作方法
- 使用Python對微信好友進(jìn)行數(shù)據(jù)分析
- 基于Python實(shí)現(xiàn)的微信好友數(shù)據(jù)分析
- Python實(shí)現(xiàn)的北京積分落戶數(shù)據(jù)分析示例
相關(guān)文章
python 動態(tài)生成變量名以及動態(tài)獲取變量的變量名方法
今天小編就為大家分享一篇python 動態(tài)生成變量名以及動態(tài)獲取變量的變量名方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01python實(shí)現(xiàn)機(jī)器學(xué)習(xí)之元線性回歸
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)機(jī)器學(xué)習(xí)之元線性回歸,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09Python實(shí)現(xiàn)在Windows平臺修改文件屬性
這篇文章主要介紹了Python實(shí)現(xiàn)在Windows平臺修改文件屬性,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03