【问题标题】:response.get() Either Doesn't Return or Times Outresponse.get() 要么不返回要么超时
【发布时间】:2017-11-25 14:11:10
【问题描述】:

我正在尝试运行以下 sn-p 代码以从指定的 url 检索数据。我尝试使用 '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)

【问题讨论】:

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


    【解决方案1】:

    您没有传递标头,特别是标头中的用户代理

    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
    

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多