“requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed

Posted

技术标签:

【中文标题】“requests.exceptions.ConnectionError: (\'Connection aborted.\', RemoteDisconnected(\'Remote end closed connection without response\',))”【英文标题】:"requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))"“requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))” 【发布时间】:2020-12-18 06:30:43 【问题描述】:

我正在尝试连接到 https://apis.digital.gob.cl/fl/feriados/2020,但在与其他 URL 完美配合的脚本上收到 requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',)) 错误。

代码:

import requests

response = requests.get('https://apis.digital.gob.cl/fl/feriados/2020')
print(response.status_code)

【问题讨论】:

【参考方案1】:

问题是网站过滤掉了没有正确User-Agent的请求,所以只使用来自MDN的随机请求:

requests.get("https://apis.digital.gob.cl/fl/feriados/2020", headers=
"User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
)

【讨论】:

以上是关于“requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed 的主要内容,如果未能解决你的问题,请参考以下文章