gpt4 book ai didi

python-3.x - Python3 使用特定 URL 请求 ConnectionError : ('Connection aborted.' , OSError ("(104, ' ECONNRESET')",))

转载 作者:行者123 更新时间:2023-12-03 21:15:37 25 4
gpt4 key购买 nike

这是我的代码。

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

然后我得到了下面的错误;
ConnectionError: ('Connection aborted.', OSError("(104, 'ECONNRESET')",))

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

最佳答案

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

另外,请确保您遵循 website's robots.txt 中提到的规则。

关于python-3.x - Python3 使用特定 URL 请求 ConnectionError : ('Connection aborted.' , OSError ("(104, ' ECONNRESET')",)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43165341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com