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

新手上路,请多包涵

我试图从“ 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))

第一次请求时发生错误,因此导致错误的不是请求频率。而且我仍然可以使用浏览器打开网站,所以我只需要让代码更像一个浏览器。除了设置用户代理外,我怎样才能做到这一点?

原文由 Jackoo 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 825
1 个回答

我知道这不是你的情况,这真的很旧,但是在搜索谷歌时我偶然发现了这个,所以我会把解决我的问题的方法留在这里:

 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)

原文由 Yuri Waki 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题