Python data analysis: a method example for obtaining the history information of a two-color ball, python Data Analysis

Source: Internet
Author: User

Python data analysis: a method example for obtaining the history information of a two-color ball, python Data Analysis

This article describes how to obtain the history of a two-color ball through Python data analysis. We will share this with you for your reference. The details are as follows:

Everyone has a mid-color two-color Ball award. For technical personnel, technical analysis can increase the chance of winning the prize. Now, the python language is used to collect historical two-color ball winning information, and then perform Prediction and Analysis.

Note:Based on the two-color ball data obtained in May 15, 2016, the total number of Lottery draws was 1940.

Preliminary Code, some of which are tedious and have better code for you to share.

#! /Usr/bin/python #-*-coding: UTF-8-*-# coding: UTF-8 # author: levycui # date: 20160513 # Description: two-color ball Information Collection import urllib2from bs4 import BeautifulSoup # Use BeautifulSoupimport osimport re # disguise as a browser login, get the webpage source code 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) handle T urllib2.HTTPError, e: print e. code print e. reason return post. read () # initialize url two-color ball homepage url = 'HTTP: // response 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 # =================================================== ========================================================== ==# obtain the information of the two-color ball on each page def getText (url): for list_num in range (1, getPageNum (url): # print list_num from the first page to the getPageNum (url) page # print the next page href = 'HTTP: // configure (list_num1_{'.html '# call the new url link # for listnum in len (list_num): page = BeautifulSoup (getPage (href) em_list = page. find_all ('em ') # match em content div_list = page. find_all ('td ', {'align': 'center '}) # match content like <td align = center> # initialize n = 0 then write the two-color ball digital information into the num.txt file 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 () writes the date information to the date.txt file 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 [:13] if len (list_num) = 0: continue elif len (list_num)> 1: fp. write (str (list_num) + '\ n') fp. close () Merge num.txtand date.txt files into the hun.txt file # format: #('2017-05-03 ', '09, 12,24, 28,29, 30,02') #('2017-05-01 ', '06, 2016,) #('2017-04-28 ', '03,') # fp01 = open ("date.txt", "r ") a = [] for line01 in fp01:. 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 ): # merge print cc fp using zip. write (str (cc) + '\ n') fp. close () #===================================================== ========================================================== if _ name __= = "_ main __": pageNum = getPageNum (url) print pageNum getpagetext = getText (url) print getpagetext

Data example:

('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')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.