Blaszard
Blaszard

Reputation: 31955

How can I make a request including "à" character in its URL in Python?

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?

Upvotes: 0

Views: 73

Answers (3)

Wonka
Wonka

Reputation: 1886

using the lib urllib to auto-encode characters.

import urllib
requests.get("http://myurl.com/"+urllib.quote_plus("àieou"))

Upvotes: 2

Olvin Roght
Olvin Roght

Reputation: 7812

Use quote_plus().

from urllib.parse import quote_plus

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

Upvotes: 1

Hans Daigle
Hans Daigle

Reputation: 364

You can try to url encode your value:

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

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

Upvotes: 0

Related Questions