response.get() 不 Return 或超时

response.get() Either Doesn't Return or Times Out

我正在尝试 运行 以下代码片段从指定的 url 检索数据。我尝试使用 'timeout=5' 参数并忽略它。

最终结果是 运行ning 脚本挂起 python 或者我收到超时错误消息?在浏览器中打开 url 似乎 return 有效 json,但我似乎无法抓取 python 中的数据。

怎么回事?

import requests

url = "http://stats.nba.com/stats/shotchartdetail?Period=0&VsConference=&LeagueID=00&LastNGames=0&TeamID=0&Position=&Location=&Outcome=&ContextMeasure=FGA&DateFrom=&StartPeriod=&DateTo=&OpponentTeamID=0&ContextFilter=&RangeType=&Season=2016-17&AheadBehind=&PlayerID=202738&EndRange=&VsDivision=&PointDiff=&RookieYear=&GameSegment=&Month=0&ClutchTime=&StartRange=&EndPeriod=&SeasonType=Regular+Season&SeasonSegment=&GameID=&PlayerPosition="

response = requests.get(url,timeout=5)
print(response)

您没有通过 headers,特别是 user-agent 在 headers

User-Agent请求header包含一个特征字符串,允许网络协议对等体识别请求软件的应用程序类型、操作系统、软件供应商或软件版本用户代理。

headers ={"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/60.0.3112.78 Chrome/60.0.3112.78 Safari/537.36"}

response = requests.get(url,headers=headers,timeout=5)
print response.text