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

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

问题描述

这是我的代码.

导入请求r = requests.get('https://academic.oup.com/journals')

然后我得到了下面的错误;

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

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

解决方案

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

❯❯❯ python3Python 3.5.2(默认,2016 年 9 月 14 日,11:28:32)[GCC 6.2.1 20160901 (Red Hat 6.2.1-1)] 在 linux 上输入帮助"、版权"、信用"或许可证"以获取更多信息.>>>进口请求>>>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<响应[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 请求 ConnectionError: ('Connection aborted.', OSError("(104, 'ECONNRESET')",)) 具有特定的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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