2017-11-25 218 views
0

我試圖運行下面的代碼片段來從指定的URL檢索數據。我嘗試使用'timeout = 5'參數以及將其保留。response.get()不返回或超時

最終的結果是,運行腳本要麼掛起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) 

回答

2

你不傳遞頭和具體用戶代理在頭

的用戶代理請求頭包含一個特徵串,其允許網絡協議對等體,以識別應用程序類型,操作系統,軟件供應商或軟件版本的請求軟件用戶代理。

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 
+0

是的,這個問題解決了,謝謝。 – trystuff

相關問題