Python3-parse#qs不';不要像预期的那样分离参数
我正在使用Python3中的Python3-parse#qs不';不要像预期的那样分离参数,python,python-3.x,url,urllib,Python,Python 3.x,Url,Urllib,我正在使用Python3中的urllib-库。守则: from urllib.parse import parse_qs parse_qs('https://www.example.com/?api-url=%2Fp%2Ftest-test-test-000761di%3Fajax%3Dtrue&api-params=%3Ft%3Dst-fs%26tc%3Dtrue') 返回字典: { 'https://www.example.com/?api-url': ['/p/test-te
urllib
-库。守则:
from urllib.parse import parse_qs
parse_qs('https://www.example.com/?api-url=%2Fp%2Ftest-test-test-000761di%3Fajax%3Dtrue&api-params=%3Ft%3Dst-fs%26tc%3Dtrue')
返回字典:
{
'https://www.example.com/?api-url': ['/p/test-test-test-000761di?ajax=true'],
'api-params': ['?t=st-fs&tc=true']
}
有人能给我解释一下这本词典是怎么编的吗
为什么..api url
和&api params
是一个键,而?ajax
,?t
,&tc
不是?在哪里可以阅读该主题?parse_qs()
只需要查询字符串。您传入了完整的URL
如果只传入查询字符串,则会得到:
>>> parse_qs('api-url=%2Fp%2Ftest-test-test-000761di%3Fajax%3Dtrue&api-params=%3Ft%3Dst-fs%26tc%3Dtrue')
{'api-url': ['/p/test-test-test-000761di?ajax=true'], 'api-params': ['?t=st-fs&tc=true']}
这是给定查询字符串的正确结果;您在输出中看到的?
、=
和&
字符将在输入查询字符串中转义
例如,api参数的转义值是%3Ft%3Dst fs%26tc%3Dtrue
;正确的解释是该字符串的无引号值,即“?t=st fs&tc=true”
然后可以再次解析这些值,以删除第二层查询字符串语法,但必须解析出查询字符串:
>>> parsed['api-url'][0].partition('?')[-1]
'ajax=true'
>>> parse_qs(parsed['api-url'][0].partition('?')[-1])
{'ajax': ['true']}
>>> parsed['api-params'][0].partition('?')[-1]
't=st-fs&tc=true'
>>> parse_qs(parsed['api-params'][0].partition('?')[-1])
{'t': ['st-fs'], 'tc': ['true']}
我曾经拆分第一个?
字符上的字符串,并将第一个字符之后的所有内容解析为查询字符串。谢谢!您能在第二段中展开一点吗(为什么这是预期的结果)?@user9115052:这些值本身可能是URL,但它们被正确引用以防止意外地被解释为查询参数。@user9115052:所以两个键的每个值都是单独的、带有查询字符串的部分URL,如果首先转义,这些查询字符串只能是另一个查询字符串的一部分。哦,那么我的url包含两个查询参数,它们本身可能包含查询参数?我主要感兴趣的是从我的url中取出/p/test-test-test-000761di
。你能告诉我正确的方向吗?如何正确地提取这些信息?