在请求中添加路径参数

时间:2021-06-25 08:53:19

标签: python python-3.x

如果我有: 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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

进行字符串连接。

url = 'https://exampleurl/'
path = 'home'
url += path
url = 'https://exampleurl/home'