我正在尝试向API发出一个简单的Get请求:
url = 'http://stats.nba.com/stats/commonplayerinfo?PlayerID=2544'
response = requests.get(url)
print('Response received')
然而,get请求只是暂停(没有任何内容打印到控制台),直到我被迫退出程序。我也尝试了urllib3,这给了我完全相同的问题。
答案 0 :(得分:1)
您需要将用户代理更改为默认请求用户代理以外的其他代理。
答案 1 :(得分:0)
鉴于您的代码被识别为机器人或爬虫,看起来他们正在推测响应。试试以下
url = 'http://stats.nba.com/stats/commonplayerinfo?PlayerID=2544'
session = requests.Session()
session.headers.update({'Host': 'stats.nba.com',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache'})
session.get(url).json()
它会给出预期的回应:
{u'resource': u'commonplayerinfo', u'resultSets': [{u'headers': [u'PERSON_ID', u'FIRST_NAME', u'LAST_NAME', u'DISPLAY_FIRST_LAST', u'DISPLAY_LAST_COMMA_FIRST', u'DISPLAY_FI_LAST', u'BIRTHDATE', u'SCHOOL', u'COUNTRY', u'LAST_AFFILIATION', u'HEIGHT', u'WEIGHT', u'SEASON_EXP', u'JERSEY', u'POSITION', u'ROSTERSTATUS', u'TEAM_ID', u'TEAM_NAME', u'TEAM_ABBREVIATION', u'TEAM_CODE', u'TEAM_CITY', u'PLAYERCODE', u'FROM_YEAR', u'TO_YEAR', u'DLEAGUE_FLAG', u'GAMES_PLAYED_FLAG', u'DRAFT_YEAR', u'DRAFT_ROUND', u'DRAFT_NUMBER'], u'rowSet': [[2544, u'LeBron', u'James', u'LeBron James', u'James, LeBron', u'L. James', u'1984-12-30T00:00:00', u'St. Vincent-St. Mary HS (OH)', u'USA', u'St. Vincent-St. Mary HS (OH)/USA', u'6-8', u'250', 14, u'23', u'Forward', u'Active', 1610612739, u'Cavaliers', u'CLE', u'cavaliers', u'Cleveland', u'lebron_james', 2003, 2017, u'N', u'Y', u'2003', u'1', u'1']], u'name': u'CommonPlayerInfo'}, {u'headers': [u'PLAYER_ID', u'PLAYER_NAME', u'TimeFrame', u'PTS', u'AST', u'REB', u'PIE'], u'rowSet': [[2544, u'LeBron James', u'2016-17', 26.4, 8.7, 8.6, 0.183]], u'name': u'PlayerHeadlineStats'}, {u'headers': [u'SEASON_ID'], u'rowSet': [[u'12003'], [u'22003'], [u'12004'], [u'22004'], [u'32004'], [u'12005'], [u'22005'], [u'32005'], [u'42005'], [u'12006'], [u'22006'], [u'32006'], [u'42006'], [u'12007'], [u'22007'], [u'32007'], [u'42007'], [u'12008'], [u'22008'], [u'32008'], [u'42008'], [u'12009'], [u'22009'], [u'32009'], [u'42009'], [u'12010'], [u'22010'], [u'32010'], [u'42010'], [u'12011'], [u'22011'], [u'32011'], [u'42011'], [u'12012'], [u'22012'], [u'32012'], [u'42012'], [u'12013'], [u'22013'], [u'32013'], [u'42013'], [u'12014'], [u'22014'], [u'32014'], [u'42014'], [u'12015'], [u'22015'], [u'32015'], [u'42015'], [u'12016'], [u'22016'], [u'32016'], [u'42016']], u'name': u'AvailableSeasons'}], u'parameters': [{u'PlayerID': 2544}, {u'LeagueID': None}]}