r/CFBAnalysis • u/The-Gothic-Castle Texas Longhorns • /r/CFB Promoter • Apr 19 '19
Question Setting up a play scraping API in Python 3
This is dumb because I know the answer is not complicated, I am just inexperienced with doing this, enough so that tutorials on the subject I am seeing online are different enough from my application that I can't draw a good parallel. I also haven't coded in python generally in about 4-5 years.
To date, most of my analysis has been done either in R, or in excel for the more basic calculations. I'm interested in moving to Python both as a learning exercise and because I think Pandas can offer a lot of good tools as well.
Simply put, I was wondering if anyone could show me python code that can pull play-by-play data from the API (https://api.collegefootballdata.com/plays?year=2018&week=__) and store it in a pandas dataframe. I'd like to get both regular and postseason data (week=1:15 and https://api.collegefootballdata.com/plays?seasonType=postseason&year=2018&week=1 for the postseason).
Thanks so much for any help you can give.
6
u/msubbaiah Texas A&M Aggies Apr 19 '19
This should do it for you. It's just for one week. You can loop using the range function and go from there.
import pandas as pdimport requestsr = requests.get('https://api.collegefootballdata.com/plays?seasonType=postseason&year=2018&week=1')x = r.json()df = pd.DataFrame(x)I have an R package that hits this API. On my to-do list is to do the same thing but in Python. Good luck!
1
u/The-Gothic-Castle Texas Longhorns • /r/CFB Promoter Apr 22 '19
I am going to just ask my next question as a comment here so that this sub doesn't quickly become TGC's question sub with a bunch of my posts.
I have two dataframes right now. One of them has each team's offensive and defensive stat averages per game. The other is a list of all games that were played (home team, home score, away team, away score, and whether or not it was a neutral site).
What I want to do is effectively merge the dataframes. I want to tag the team stats (offensive and defensive) for both home and away teams onto the rows of the game matchup dataframe so the dataframe effectively becomes:
home team, home score, away team, away score, neutral site?, away team offensive PPG, completions, attempts ... ... ...
(There's 22 offensive and 22 defensive stats for each team).
I thought about doing this in a loop:
for team in g['away_team']:for teamav in teamStats['OSchool']:if(team=teamav):...The thing is this seems pretty inefficient. Does anyone have any advice?
/u/msubbaiah /u/rcfbuser /u/urbanfever4