简体   繁体   中英

Python ConnectionError: ('Connection aborted.', OSError(“(10060, 'WSAETIMEDOUT')”))

I have looked for lots of questions on this issue but none of them contains a solution that works for me yet. Here is my code:

import requests
import pandas as pd
shot_chart_url = 'https://stats.nba.com/stats/shotchartdetail?AheadBehind=&CFID=&CFPARAMS=&ClutchTime=&Conference=&ContextFilter=&ContextMeasure=FGM&DateFrom=&DateTo=&Division=&EndPeriod=10&EndRange=28800&GROUP_ID=&GameEventID=&GameID=&GameSegment=&GroupID=&GroupMode=&GroupQuantity=5&LastNGames=0&LeagueID=00&Location=&Month=0&OnOff=&OpponentTeamID=0&Outcome=&PORound=0&Period=0&PlayerID={PlayerID}&PlayerID1=&PlayerID2=&PlayerID3=&PlayerID4=&PlayerID5=&PlayerPosition=&PointDiff=&Position=&RangeType=0&RookieYear=&Season={Season}&SeasonSegment=&SeasonType={SeasonType}&ShotClockRange=&StartPeriod=1&StartRange=0&StarterBench=&TeamID=0&VsConference=&VsDivision=&VsPlayerID1=&VsPlayerID2=&VsPlayerID3=&VsPlayerID4=&VsPlayerID5=&VsTeamID='\
                  .format(PlayerID=201935, Season='2019-20', SeasonType='Regular+Season')

headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
response = requests.get(shot_chart_url, headers=headers)
headers = response.json()['resultSets'][0]['headers']
shots = response.json()['resultSets'][0]['rowSet']
shot_df = pd.DataFrame(shots, columns=headers)

I had tried fake_useragent but it didn't work. Lots of solutions indicates that using the headers could fix the problem but doesn't work for me.

There is a website that explains the detail of how to get shotchartdetail data by nba_api: https://datavizardry.com/2020/01/28/nba-shot-charts-part-1/

from nba_api.stats.endpoints import shotchartdetail
import json

response = shotchartdetail.ShotChartDetail(
    team_id=0,
    player_id=201935,
    season_nullable='2019-20',
    season_type_all_star='Regular Season'
)

content = json.loads(response.get_json())

Output:

{"resource": "shotchartdetail", "parameters": {"LeagueID": "00", "Season": "2019-20", "SeasonType": "Regular Season", "TeamID": 0, "PlayerID": 201935, "GameID": null, "Outcome": null, "Location": null, "Month": 0, "SeasonSegment": null, "DateFrom": null, "DateTo": null, "OpponentTeamID": 0, "VsConference": null, "VsDivision": null, "Position": null, "RookieYear": null, "GameSegment": null, "Period": 0, "LastNGames": 0, "ClutchTime": null, "AheadBehind": null, "PointDiff": null, "RangeType": null, "StartPeriod": null, "EndPeriod": null, "StartRange": null, "EndRange": null, "ContextFilter": "", "ContextMeasure": "PTS"}, "resultSets": [{"name": "Shot_Chart_Detail", "headers": ["GRID_TYPE", "GAME_ID", "GAME_EVENT_ID", "PLAYER_ID", "PLAYER_NAME", "TEAM_ID", "TEAM_NAME", "PERIOD", "MINUTES_REMAINING", "SECONDS_REMAINING", "EVENT_TYPE", "ACTION_TYPE", "SHOT_TYPE", "SHOT_ZONE_BASIC", "SHOT_ZONE_AREA", "SHOT_ZONE_RANGE", "SHOT_DISTANCE", "LOC_X", "LOC_Y", "SHOT_ATTEMPTED_FLAG", "SHOT_MADE_FLAG", "GAME_DATE", "HTM", "VTM"], "rowSet": [["Shot Chart Detail", "0021900015", 350, 201935, "James Harden", 1610612745, "Houston Rockets", 2, 2, 6, "Made Shot", "Driving Layup Shot", "2PT Field Goal", "Restricted Area", "Center(C)", "Less Than 8 ft.", 3, 22, 21, 1, 1, "20191024", "HOU", "MIL"], ["Shot Chart Detail", "0021900015", 353, 201935, "James Harden", 1610612745, "Houston Rockets", 2, 1, 40, "Made Shot", "Step Back Jump shot", "3PT Field Goal", "Above the Break 3", "Center(C)", "24+ ft.", 28, -43, 278, 1, 1, "20191024", "HOU", "MIL"], ["Shot Chart Detail", "0021900032", 8, 201935, "James Harden", 1610612745, "Houston Rockets", 1, 11, 38, "Made Shot", "Driving Layup Shot", "2PT Field Goal", "Restricted Area", "Center(C)", "Less Than 8 ft.", 1, -14, 7, 1, 1, "20191026", "HOU", "NOP"], ["Shot Chart Detail", "0021900032", 115, 201935, "James Harden",
....
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM