Scrape Powerball Number Frequency



>> Monday, August 30, 2010

I know that buying lottery tickets is a total waste of money. I also know that there is nothing you can do to increase the odds of hitting a Powerball jackpot (1 in 195,249,054). However, just for fun, I thought I'd fire off a quick groovy script that will read the frequencies from the Powerball website.

Once I have all the numbers I could sort the lists to find out which numbers are most frequently picked (or least frequently picked). I'm sure there is other statistical analysis that you could do too if you wanted. You could also watch the frequency trends over time (though this historic list of numbers may be better at that)

Here's the simple script:

import org.cyberneko.html.parsers.SAXParser;

//initialize
def maxPowerBall = 39
def whiteBallFreq = [:]
def pbFreq = [:]

//read the numbers
def url = 'http://powerball.com/powerball/pb_frequency.asp'
def html = new XmlSlurper(new SAXParser()).parse(url)

//create the lists from the HTML
html.BODY.TABLE.TBODY.TR[3].TD[1].TABLE.TBODY.TR[4].TD[1].TABLE.TBODY.TR[2..-2].each{nextRow ->
def cols = nextRow.children()
def ball = Integer.parseInt(cols[0].text())

whiteBallFreq[ball] = parsePossiblyBlankNumber(cols[1].text())
if(ball <= maxPowerBall){
pbFreq[ball] = parsePossiblyBlankNumber(cols[2].text())
}
}

def int parsePossiblyBlankNumber(String value) {
if(value.trim().length() == 0){
return 0;
} else {
return Integer.parseInt(value)
}
}

The import is found here. The long "html.BODY.TABLE..." line is needed because of how the Powerball website is set up.

I put in the parsePossiblyBlankNumber method since the website uses blanks instead of 0s when there are no frequencies.

Leave a comment if you have any questions or suggestions!

Post a Comment

  © Blogger template Webnolia by Ourblogtemplates.com 2009

Back to TOP