web-dev-qa-db-ja.com

Python3は特定のURLでConnectionError:( 'Connection aborted。'、OSError( "(104、 'ECONNRESET')"、))をリクエストします

これは私のコードです。

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

次に、以下のエラーが発生しました。

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

エラーが発生したのはなぜですか?私は何をすべきか?リクエストは https://www.google.com のような他のURLでもうまく機能します

7
liliput

HTTPヘッダーでユーザーエージェントを指定してみてください。

❯❯❯ 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 に記載されているルールに従ってください。

10
Nehal J Wani