Skip to content

Instantly share code, notes, and snippets.

Created August 17, 2017 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/36ae106e11953b04db81c0f8eaaafaba to your computer and use it in GitHub Desktop.
Save anonymous/36ae106e11953b04db81c0f8eaaafaba to your computer and use it in GitHub Desktop.
for game in gamedata:
while True:
try:
req = requests.get("http://www.espn.com/mens-college-basketball/playbyplay?gameId={!s}".format(game[0]))
print('Game found: ' + game[0])
print(req) # gives html status code
except:
# Wait 1 minute and try again
time.sleep(60)
if req.status_code == 200:
break
print("Error. Retrying...")
time.sleep(15)
home = ''
away = ''
for team in game[2]:
if team[1].lower() == 'home':
home = team[2]
else:
away = team[2]
soup = BeautifulSoup(req.content, 'html.parser')
header = 0 # Delete 1 from game index if there is a header
# Using i to create an index for each game - may be easier later than the rowid from sqlite
for i, row in enumerate(soup.article.find_all('tr')):
if row.th:
header += 1
continue # skip header rows
splittime = row.find_all('td','time-stamp')[0].string.split(':')
gametime = int(splittime[0])*60 + int(splittime[1]) # MM:SS to seconds
try:
actor = int(re.search(r'\/([0-9]+)\.', row.find_all('img')[0]['src'])[1])
except IndexError:
# At least one game doesn't have team logos
actor = -1
badlogos.append(game[0])
event = row.find_all('td','game-details')[0].string
splitscore = row.find_all('td','combined-score')[0].string.split('-') # MM:SS to seconds
awayscore = int(splitscore[0])
homescore = int(splitscore[1])
results.append([int(game[0]), game[1], i-header, gametime, actor, event, away, home, awayscore, homescore])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment