【问题标题】:convert string to http string format [duplicate]将字符串转换为http字符串格式[重复]
【发布时间】:2020-12-05 11:12:21
【问题描述】:

我需要发出 api 请求,我需要转换一个字符串,例如:

"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)

谢谢

【问题讨论】:

  • 你只是想删除空格吗?
  • @jon 我觉得如果有=、>这样的特殊字符,也应该转换一下
  • 这就是您要找的东西吗? stackoverflow.com/questions/5607551/…
  • @Hedy 我的想法是,您先对要结束编码的每个特殊字符执行 split() 然后 join() 函数,但是这样做效率如何

标签: python http http-headers special-characters


【解决方案1】:

喜欢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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2019-04-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 2014-07-06
    相关资源
    最近更新 更多