将字符串转换为http字符串格式

问题描述

我需要发出和api请求,并且需要转换一个strin像这样:

"hello i am a special string + i contain special characters?"

转换为以下请求格式:

"hello%20i%20am%20a%20special ... ecc"

python是否有一个库可以帮助我?

这是我的代码

import http.client
conn = http.client.HTTPSConnection("address:port")
conn.request(method="GET",url="/my/target/site?info1=this needs to be converted&info2=also this???",headers=headers)

谢谢

解决方法

How to convert a url string to safe characters with python? 还有How to urlencode a querystring in Python?

您可以使用

import urllib
urllib.parse.quote_plus('string here')