Skip to content

Instantly share code, notes, and snippets.

@oztalha
Last active January 27, 2017 14:06
Show Gist options
  • Save oztalha/65cac41fea69484158f267d236101d1a to your computer and use it in GitHub Desktop.
Save oztalha/65cac41fea69484158f267d236101d1a to your computer and use it in GitHub Desktop.
Python script to scrape NBA game logs (saves each season as a separate html file)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time
driver = webdriver.Chrome()
#driver.get('http://stats.nba.com/teams/gamelogs/#!?Season=ALLTIME&SeasonType=Regular%20Season')
for year in range(2016,1945,-1):
url = 'http://stats.nba.com/teams/gamelogs/#!?Season='+str(year)+'-'+format((year+1)%100,'02d')+'&SeasonType=Regular%20Season'
driver.get(url)
time.sleep(3)
driver.get(url) #page needs to be refreshed
for i in range(50):
time.sleep(3)
try:
#driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.find_element_by_class_name('table-addrows__button').click()
except:
pass
with open('session'+str(year)+'.htm', 'w') as out:
out.write(driver.page_source)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment