python3请求使用quote而不是quote_plus

2024-03-16

我使用 Python 3 和requests http://docs.python-requests.org/en/master/用于查询 REST 服务的模块/库。

看来请求默认使用urllib.parse.quote_plus()对于 urlencoding,即空格转换为+.

然而,我查询的 REST 服务将此误解为and。所以我需要将空格编码为%20,即使用urllib.parse.quote()反而。

有没有一种简单的方法可以通过请求来做到这一点?我在文档中找不到任何选项。


事实证明你可以!

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

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

文档在这里 https://3.python-requests.org/_modules/requests/utils/ The requote_uri方法位于页面的中间位置。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python3请求使用quote而不是quote_plus 的相关文章

随机推荐