Skip to content

Instantly share code, notes, and snippets.

@bcarpio
Created December 2, 2012 06:25
Show Gist options
  • Save bcarpio/4187244 to your computer and use it in GitHub Desktop.
Save bcarpio/4187244 to your computer and use it in GitHub Desktop.
MegaMillions Winning Numbers Scraper
#!/usr/bin/python
# vim: set expandtab:
import urllib2
from BeautifulSoup import BeautifulSoup
win_dict = {}
page_num = 1
total_pages = 63
while True:
if page_num > total_pages: break
page_num = str(page_num)
soup = BeautifulSoup(urllib2.urlopen('http://www.usamega.com/mega-millions-history.asp?p='+page_num).read())
for row in soup('table')[4].findAll('tr'):
tds = row('td')
if tds[1].a is not None:
win_dict['date'] = tds[1].a.string
if tds[3].b is not None:
num_list = []
number_list = tds[3].b.string.split('·')
for num in number_list:
num_list.append(int(num))
win_dict['numbers'] = num_list
mega_number = tds[3].strong.string
win_dict['mega_number'] = int(mega_number)
print win_dict
page_num = int(page_num)
page_num += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment