python3 requests use quote instead of quote_plus

可紊 提交于 2019-12-04 12:34:06

问题


I use Python 3 and the requests module/library for querying a REST service.

It seems that requests by default uses urllib.parse.quote_plus() for urlencoding, i.e. spaces are converted to +.

However the REST service I query misinterpretes this as and. So I need to encode spaces as %20, i.e. use urllib.parse.quote() instead.

Is there an easy way to do this with requests? I couldn't find any option in the documentation.


回答1:


It turns out you can!

from requests.utils import requote_uri
url = "https://www.somerandom.com/?name=Something Cool"
requote_uri(url)

'https://www.somerandom.com/?name=Something%20Cool'

documentation here The requote_uri method is about halfway down the page.



来源:https://stackoverflow.com/questions/42349000/python3-requests-use-quote-instead-of-quote-plus

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