小编and*_*ngo的帖子

Python get 引发 HTTPError 400 Client Error,但手动访问 URL 后,get 暂时有效

当我在 iPython (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()
Run Code Online (Sandbox Code Playgroud)

我正进入(状态:

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
Run Code Online (Sandbox Code Playgroud)

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

这段代码已经为我工作了数百次,甚至数千次都没有问题。我该如何解决这个错误? …

python json ipython http-error python-requests

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

http-error ×1

ipython ×1

json ×1

python ×1

python-requests ×1