问题描述
我使用Python 3和请求模块/库查询REST服务.
I use Python 3 and the requests module/library for querying a REST service.
默认情况下,请求似乎使用urllib.parse.quote_plus()
进行urlencoding,即空格转换为+
.
It seems that requests by default uses urllib.parse.quote_plus()
for urlencoding, i.e. spaces are converted to +
.
但是我查询的REST服务将其误解为and
.所以我需要将空格编码为%20
,即改用urllib.parse.quote()
.
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.
推荐答案
事实证明您可以!
from requests.utils import requote_uri
url = "https://www.somerandom.com/?name=Something Cool"
requote_uri(url)
'https://www.somerandom.com/?name=Something%20Cool'
此处的文档 requote_uri
方法大约位于页面.
documentation here The requote_uri
method is about halfway down the page.
这篇关于python3请求使用引号而不是quote_plus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!