Ruby vs Python获取请求

时间:2017-04-19 06:01:07

标签: python ruby

我正在尝试用红宝石向NBA API发出GET请求。我已经尝试了requests库,但请求在5分钟后挂起。

当我将python的require "unirest" base_url = "http://stats.nba.com/stats" team_endpoint = "/teamgamelog" params = {:TeamID => "1610612739", :Season => "2016-17", :SeasonType => "Playoffs"} HEADERS = {'user-agent'=> 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36', 'Accept'=> "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" } response = Unirest.get "#{base_url}#{team_endpoint}", headers: HEADERS, parameters: params 库用于相同的url +端点时,它可以工作。以下是我为每个人尝试的内容:

import requests

base_url = 'http://stats.nba.com/stats'
HEADERS = {'user-agent': ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) '
               'AppleWebKit/537.36 (KHTML, like Gecko) '
               'Chrome/45.0.2454.101 Safari/537.36'),}
end_point = '/teamgamelog'
PARAMS = {'TeamID': '1610612739', 'Season': '2016-17', 'SeasonType': 'Playoffs'}

r = requests.get(base_url+end_point, headers=HEADERS, params=PARAMS)

以上代码不起作用(连接最终超时)。以下python代码有效:

{{1}}

有关解决红宝石案的任何建议吗?

0 个答案:

没有答案