Python get引发HTTPError 400客户机错误,但在手动访问URL之后,get将临时工作

2024-05-20 15:00:41 发布

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

在I Python(Python 2.7)中运行此代码时:

from requests import get
_get = get('http://stats.nba.com/stats/playergamelog', params={'PlayerID': 203083, 'Season':'2015-16', 'SeasonType':'Regular Season'})
print _get.url
_get.raise_for_status()
_get.json()

我得到:

http://stats.nba.com/stats/playergamelog?PlayerID=203083&Season=2015-16&SeasonType=Regular+Season
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
<ipython-input-5-8f8343b2c4cd> in <module>()
      1 _get = get('http://stats.nba.com/stats/playergamelog', params={'PlayerID': 203083, 'Season':'2015-16', 'SeasonType':'Regular Season'})
      2 print _get.url
----> 3 _get.raise_for_status()
      4 _get.json()

/Library/Python/2.7/site-packages/requests/models.pyc in raise_for_status(self)
    849 
    850         if http_error_msg:
--> 851             raise HTTPError(http_error_msg, response=self)
    852 
    853     def close(self):

HTTPError: 400 Client Error: Bad Request

但是,如果我转到浏览器中的url,它就会工作。然后,当我回到代码并在浏览器中手动访问URL(iPython正在运行的Chrome)后再次运行它时,代码运行没有错误。然而,它可能会返回到提高顺序执行中的错误。

这段代码已经为我工作了数百次甚至数千次,没有任何问题。如何修复此错误?

谢谢。


Tags: 代码comhttpurlforgetstatsstatus
1条回答
网友
1楼 · 发布于 2024-05-20 15:00:41

HTTPError: 400 Client Error: Bad Request表示您的请求有错误。我认为服务器可能会检查HTTP请求中的一些头,例如user-agent

所以我尝试将用户代理头设置为模仿Firefox:

# No User-Agent
>>> _get = get('http://stats.nba.com/stats/playergamelog', params={'PlayerID': 203082, 'Season':'2015-16', 'SeasonType':'Regular Season'})
>>> _get.raise_for_status()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\requests\models.py", line 840, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http://stats.nba.com/stats/playergamelog?PlayerID=203082&Season=2015-16&SeasonType=Regular+Season

# This time, set user-agent to mimic a desktop browser
>>> headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0'}
>>> _get = get('http://stats.nba.com/stats/playergamelog', params={'PlayerID': 203082, 'Season':'2015-16', 'SeasonType':'Regular Season'}, headers=headers)
>>> _get.raise_for_status()
>>>
# no error

在浏览器中访问URL后它可以工作的原因是缓存。

根据Alastair McCormack的说法,stats.nba.com由Akamai CDN前置,因此缓存可能发生在边缘,由查询字符串/URI而不是外部头“变化”。对该URI作出有效响应后,它将由服务于该客户机的CDN边缘节点缓存。

因此,当您在浏览器中访问url后运行代码时,CDN将返回缓存的响应。在这种情况下,不会筹集400英镑。

相关问题 更多 >