【问题标题】:What is the proper way to fill in URL params http://myhost/path/{param1}/{param2} in Python?在 Python 中填写 URL 参数 http://myhost/path/{param1}/{param2} 的正确方法是什么?
【发布时间】:2023-02-21 00:27:22
【问题描述】:

我有 REST api 的 URLs

http://myhost/path/{param1}/{param2}

我知道我可以自己进行 urlencode 和组合,可能是像requests.get alsready 这样的功能吗?

【问题讨论】:

  • 只是常规的 Python 字符串格式? requests.get(f'http://myhost/path/{param1}/{param2}') 会将名为param1param2 的变量的值插入到适当的位置。

标签: python-3.x python-requests


【解决方案1】:
param1 = 'spaghetti'
param2 = 'meatballs'

print(f'http://myhost/path/{param1}/{param2}')
# http://myhost/path/spaghetti/meatballs

【讨论】:

    猜你喜欢
    • 2015-01-13
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 2017-09-07
    • 2014-03-27
    相关资源
    最近更新 更多