问题
I have an encoding problem : When I type the caracter 'é' in an input in a web browser, it is posted as %E9, and it works fine. on the other hand, when I try to post a request using Python and requests library, it is sent as %C3%A9.
How could I solve the problem ?
Here is the code that does not work
requests.post("http://localhost", message = {"text":'é'})
Thanks
回答1:
%C3%A9
is url-encoded version of utf-8 encoded string:
>>> u'é'.encode('utf-8')
'\xc3\xa9'
>>> urllib.quote(u'é'.encode('utf-8'))
'%C3%A9'
Explicitly encode the string with latin-1
encoding (or similar):
>>> u'é'.encode('latin1')
'\xe9'
>>> urllib.quote(u'é'.encode('latin-1'))
'%E9'
requests.post("http://localhost", message={"text": u'é'.encode('latin-1')})
来源:https://stackoverflow.com/questions/25965362/http-post-accents-encoding