Ruby与Python GET请求

2024-06-16 10:58:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用ruby向nbaapi发出GET请求。我尝试了unirest库,但请求在5分钟后挂起。你知道吗

当我对同一个url+端点使用python的requests库时,它可以工作。以下是我为每个人所做的努力:

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

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

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)

关于解决鲁比案有什么建议吗?你知道吗


Tags: comhttpurlbasestatsparamsrequestsendpoint