Python3请求具有特定URL的ConnectionError :(“连接已中止.",OSError(“(104,'ECONNRESET')",))) [英] Python3 Requests ConnectionError: ('Connection aborted.', OSError("(104, 'ECONNRESET')",)) with a specific URL

查看:802
本文介绍了Python3请求具有特定URL的ConnectionError :(“连接已中止.",OSError(“(104,'ECONNRESET')",)))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.

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

然后我得到下面的错误;

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

为什么会出现错误? 我该怎么办? 该请求可与 https://www.google.com

之类的其他URL很好地配合使用

解决方案

尝试在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]>

此外,请确保您遵循网站的robots.txt

This is my code.

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

then I got the error below;

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

Why I got the error? What should I do? the Request works well with other URLs like https://www.google.com

解决方案

Try specifying a user-agent in the HTTP header:

❯❯❯ 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]>

Also, make sure you follow the rules mentioned in the website's robots.txt

这篇关于Python3请求具有特定URL的ConnectionError :(“连接已中止.",OSError(“(104,'ECONNRESET')",)))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆