URI encoding in Python Requests package

回眸只為那壹抹淺笑 提交于 2019-12-10 08:50:16

问题


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:


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'


来源:https://stackoverflow.com/questions/46783078/uri-encoding-in-python-requests-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!