首页 > 解决方案 > 在请求中添加路径参数

问题描述

如果我有: https://exampleurl/{path}这样的 URL,那么requests我应该使用模块中的哪些功能来填写路径?我知道我可以传递类似的东西:

query = {'q0':'path_params','q1':'hello', 'q2':'world'}

并将其传递给requests.get(url, params=query). 但这会产生这样的结果:

https://exampleurl/?q1=hello&q2=world

代替https://exampleurl/path_params?q1=hello&q2=world

标签: pythonpython-3.x

解决方案


您可以在 url 上做字符串格式来填写路径参数。

url = 'https://exampleurl/{path_param}'
api_call = url.format(path)

推荐阅读