response.get()不返回或超时

时间:2017-11-25 14:11:10

标签: python python-3.x python-requests response urllib

我正在尝试运行以下代码片段以从指定的网址中检索数据。我尝试使用'timeout = 5'参数以及将其删除。

最终结果是运行脚本要么挂起python,要么我收到超时错误消息?在浏览器中打开url似乎返回有效的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)

1 个答案:

答案 0 :(得分:2)

您没有在标题中传递标题和特定用户代理

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

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