Created
December 2, 2012 06:25
-
-
Save bcarpio/4187244 to your computer and use it in GitHub Desktop.
MegaMillions Winning Numbers Scraper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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