Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 Requests ConnectionError: ('Connection aborted.', OSError("(104, 'ECONNRESET')",)) with a specific URL

This is my code.

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

then I got the error below;

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

Why I got the error? What should I do? the Request works well with other URLs like https://www.google.com

like image 885
liliput Avatar asked Apr 02 '17 06:04

liliput


1 Answers

Try specifying a user-agent in the 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]>

Also, make sure you follow the rules mentioned in the website's robots.txt

like image 101
Nehal J Wani Avatar answered Oct 15 '22 03:10

Nehal J Wani