Skip to content

Commit

Permalink
pass user agent to get request
Browse files Browse the repository at this point in the history
  • Loading branch information
bdilday committed Aug 30, 2024
1 parent 691dbe3 commit 753fd7e
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions pybaseball/top_prospects.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,43 @@ def top_prospects(teamName=None, playerType=None):
Retrieves the top prospects by team or leaguewide. It can return top prospect pitchers, batters, or both.
ARGUMENTS
team: The team name for which you wish to retrieve top prospects. There must be no whitespace. If not specified,
team: The team name for which you wish to retrieve top prospects. There must be no whitespace. If not specified,
the function will return leaguewide top prospects.
playerType: Either "pitchers" or "batters". If not specified, the function will return top prospects for both
playerType: Either "pitchers" or "batters". If not specified, the function will return top prospects for both
pitchers and batters.
"""
if teamName == None:
url = "https://www.mlb.com/prospects/stats/top-prospects"
else:
mlbTeamId = teamid_lookup.mlb_team_id(teamName)
url = f"https://www.mlb.com/prospects/stats?teamId={mlbTeamId}"

res = requests.get(url, timeout=None).content

res = requests.get(
url,
timeout=None,
headers={
"UserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
"AppleWebKit/537.36 (KHTML, like Gecko) C"
"hrome/104.0.5112.79 Safari/537.36"
},
).content
prospectList = pd.read_html(res)

if playerType == "batters":
topBattingProspects = postprocess(prospectList[0])
return topBattingProspects
elif playerType == "pitchers":
elif playerType == "pitchers":
topPitchingProspects = postprocess(prospectList[1])
return topPitchingProspects
elif playerType == None:
topProspects = pd.concat(prospectList)
topProspects.sort_values(by=['Rk'], inplace = True)
topProspects.sort_values(by=["Rk"], inplace=True)
topProspects = postprocess(topProspects)
return topProspects


def postprocess(prospectList):
prospectList = prospectList.drop(list(prospectList.filter(regex = 'Tm|Unnamed:*')), axis = 1)
return prospectList
def postprocess(prospectList):
prospectList = prospectList.drop(
list(prospectList.filter(regex="Tm|Unnamed:*")), axis=1
)
return prospectList

0 comments on commit 753fd7e

Please sign in to comment.