URI encoding in Python Requests package

2019-07-17 13:12发布

I am using python requests package to get results from a API and the URL contains + sign in it. but when I use requests.get, the request is failing as the API is not able to understand + sign. how ever if I replace + sign with %2B (URI Encoding) the request is successful.

Is there way to encode these characters, so that I encode the URL while passing it to the requests package

Error: test user@gmail.com does not exist
API : https://example.com/test+user@gmail.com

1条回答
贪生不怕死
2楼-- · 2019-07-17 13:40

You can use requests.utils.quote (which is just a link to urllib.parse.quote) to convert your text to url encoded format.

>>> import requests
>>> requests.utils.quote('test+user@gmail.com')
'test%2Buser%40gmail.com'
查看更多
登录 后发表回答