Python Requests包中的URI编码 [英] URI encoding in Python Requests package

查看:538
本文介绍了Python Requests包中的URI编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python请求包从API获取结果,并且URL包含+登录.但是当我使用request.get时,请求失败,因为API无法理解+号.如果我用%2B(URI编码)替换+号,请求将如何成功.

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.

是否可以对这些字符进行编码,以便在将URL传递到请求包时对URL进行编码

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

推荐答案

您可以使用requests.utils.quote(它只是" rel ="noreferrer > urllib.parse.quote )将您的文本转换为url编码格式.

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'

这篇关于Python Requests包中的URI编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆