Python3请求ConnectionError:('连接已中止。',OSError("(104,' ECONNRESET')",))具有特定的URL

时间:2017-04-02 06:36:29

标签: python-3.x python-requests anaconda

这是我的代码。

import requests
r = requests.get('https://academic.oup.com/journals')

然后我收到了以下错误;

ConnectionError: ('Connection aborted.', OSError("(104, 'ECONNRESET')",))

为什么我收到错误? 我该怎么办? 该请求适用于其他网址,例如https://www.google.com

1 个答案:

答案 0 :(得分:6)

尝试在HTTP标头中指定用户代理:

❯❯❯ python3
Python 3.5.2 (default, Sep 14 2016, 11:28:32) 
[GCC 6.2.1 20160901 (Red Hat 6.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> headers = requests.utils.default_headers()
>>> headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
>>> r = requests.get('https://academic.oup.com/journals', headers=headers)
>>> r
<Response [200]>

另外,请确保遵循website's robots.txt

中提到的规则