0

I use the requests module in Python to fetch a result of a web page. However, I found that if the URL includes a character à in its URL, it issues the UnicodeDecodeError:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 27: invalid continuation byte

Strangely, this only happens if I also add a space in the URL. So for example, the following does not issue an error.

requests.get("http://myurl.com/àieou")

However, the following does:

requests.get("http://myurl.com/àienah aie")

Why does it happen and how can I make the request correctly?

3 Answers 3

2

using the lib urllib to auto-encode characters.

import urllib
requests.get("http://myurl.com/"+urllib.quote_plus("àieou"))
Sign up to request clarification or add additional context in comments.

8 Comments

That worked, thanks. Though it was urllib.parse.quote in Python 3.
Hmmm it still didn't work if the URL includes a space.
Its should work and replace space with +. There is some different .quote and .quote_plus. I dont know it currently at the moment, quote_plus is more hardcore.
May be you should use quote_plus instead of quote?
Yes @OlvinRoght , thought I used quote_plus
|
1

Use quote_plus().

from urllib.parse import quote_plus

requests.get("http://myurl.com/" + quote_plus("àienah aie"))

Comments

0

You can try to url encode your value:

requests.get("http://myurl.com/%C3%A0ieou")

The value for à is %C3%A0 once encoded.

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.