1

I'm a new user of python. I don't know why but requests always throws an InvalidURL exception:

>>> import requests
>>> r = requests.get('https://www.google.es/')

The output:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/requests/models.py", line 380, in prepare_url
    scheme, auth, host, port, path, query, fragment = parse_url(url)
  File "/usr/lib/python3/dist-packages/urllib3/util/url.py", line 392, in parse_url
    return six.raise_from(LocationParseError(source_url), None)
  File "<string>", line 3, in raise_from
urllib3.exceptions.LocationParseError: Failed to parse: https://www.google.es/

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/dist-packages/requests/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/requests/sessions.py", line 516, in request
    prep = self.prepare_request(req)
  File "/usr/local/lib/python3.7/dist-packages/requests/sessions.py", line 459, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/usr/local/lib/python3.7/dist-packages/requests/models.py", line 314, in prepare
    self.prepare_url(url, params)
  File "/usr/local/lib/python3.7/dist-packages/requests/models.py", line 382, in prepare_url
    raise InvalidURL(*e.args)
requests.exceptions.InvalidURL: Failed to parse: https://www.google.es/

This error is independent of the url I give. How do I handle this?

The version of Python is 3.7.7 and 2.23.0 for requests.

Best regards.

8
  • try r = requests.request('GET','https://www.google.es/') Commented Apr 7, 2020 at 9:04
  • @InfinityTM, I tried with 'GET' as first parameter but the exception was: requests.exceptions.InvalidURL: Failed to parse: GET Commented Apr 7, 2020 at 9:10
  • Works for me. Please post a proper minimal reproducible example. NB: you may want to make sure that at this point of your code requests is really what you expect (just add a print(requests) before the call and check your have). Commented Apr 7, 2020 at 9:12
  • its the issue with urllib3 Commented Apr 7, 2020 at 9:15
  • try installing requests v2.21.0 Commented Apr 7, 2020 at 9:16

3 Answers 3

11

You faced Error due to the New version of urllib3 (some users tends to face this issue).

The error is not due to requests but issue is rather in urllib3 (new ver) that gets installed when installing requests 2.21.0+.
To avoid this either try updating urllib3:

python -m pip install --upgrade urllib3

or install the requests v2.21.0:

pip uninstall requests # to remove current version
pip install requests==2.21.0
  • Just downgrade it to v2.21.0 version
Sign up to request clarification or add additional context in comments.

Comments

1

See if you have a hidden character in your URL.

I've wasted tons of time and that was the problem.. this can happen when you copy-paste the URL from somewhere.

enter image description here

2 Comments

Thank you @tomer-cohen but it was a problem with the version of urllib3 for me as it was noticed in the previous comment.
sure, just added this because this was my problem, and I didn't find any solution
0

it's happen sometimes when the URL is not the valid one. I have this error and after hours found that I have a small spaces between each / on the URL... so I suggest to write the URL again on requests.get for no get this mistake..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.