打开图片时,Python3请求ConnectionResetError(10054)

时间:2018-06-09 05:54:14

标签: python python-requests web-crawler

我试图从“http://xxx.jpg”等网站下载图片。

代码:

headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'}
url='http://xxx.jpg'
response = requests.get(url,headers=headers)
downloadFunction()

错误写道:

requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

第一个请求发生错误,因此不是导致错误的请求频率。我仍然可以使用浏览器打开网站,所以我只需要代码更像浏览器。除了设置用户代理之外,我该如何实现呢?

2 个答案:

答案 0 :(得分:0)

当服务器通过https托管在我的机器上并且SSL证书未正确安装时,我出现了此错误。

按照说明正确安装服务器的证书即可解决问题:

https://coderead.wordpress.com/2014/08/07/enabling-ssl-for-self-hosted-nancy/ https://www.cloudinsidr.com/content/how-to-install-the-most-recent-version-of-openssl-on-windows-10-in-64-bit/

答案 1 :(得分:0)

我知道这不是您的情况,而且确实很老,但是在搜索google时,我偶然发现了这个问题,因此我将在此留下解决问题的方法:

test_link = "https://www.bbb.org/washington-dc-eastern-pa/business-reviews/online-education/k12-inc-in-herndon-va-190911943/#sealclick"
page = requests.get(test_link)

我得到了错误:

requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

所以它不是多个连接,我认为问题是标题,一旦我将标题放错了,这就是后面的代码:

test_link = "https://www.bbb.org/washington-dc-eastern-pa/business-reviews/online-education/k12-inc-in-herndon-va-190911943/#sealclick"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0",
    "Accept-Encoding": "*",
    "Connection": "keep-alive"
}
page = requests.get(test_link, headers=headers)