Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中解析查询字符串参数?_Python_Rest - Fatal编程技术网

如何在python中解析查询字符串参数?

如何在python中解析查询字符串参数?,python,rest,Python,Rest,我正在开发RESTAPI并使用python。对于get请求(下面的示例), 我假设,任何进行调用的人都会对URL进行URL编码,在python中解码和读取查询参数的正确方法是什么 "https://someurl.com/query_string_params?id=1&type=abc" import requests import urllib def get(): //parse query string parameters here

我正在开发RESTAPI并使用python。对于get请求(下面的示例), 我假设,任何进行调用的人都会对URL进行URL编码,在python中解码和读取查询参数的正确方法是什么

"https://someurl.com/query_string_params?id=1&type=abc"

import requests
import urllib

  def get():
        
    //parse query string parameters here
        

下面是一个如何拆分URL并获取查询参数的示例:

import urllib.parse

url='https://someurl.com/query_string_params?id=1&type=abc'

url_parts = urllib.parse.urlparse(url)

print( f"{url_parts=}" )

query_parts = urllib.parse.parse_qs(url_parts.query)

print( f"{query_parts=}" )
结果:

url_parts=ParseResult(scheme='https', netloc='someurl.com', path='/query_string_params', params='', query='id=1&type=abc', fragment='')
query_parts={'id': ['1'], 'type': ['abc']}

此处有文档

这是否回答了您的问题?阅读文档?e、 g.@SvenEberth-谢谢。我的代码中是否需要URL解码?