我使用以下代码解析URL:
url3 = "https://flyingcar.org/dfr/?d=34u&f=56&dossier=ZT18174&document=US1234567&docs='US1','US2','US3'#{'para1':'UK','para2':'Ireland'}"
parsed = urlparse(url3)
print('url:n',url3)
captured_values = parse_qs(parsed.query)
print(f'parse_qs of querys: {captured_values} type: {type(captured_values)}')
print(f'here the parameters: {parsed.fragment} type: {type(parsed.fragment)}')
一切都很好。我不知道的是如何构建URL以传递变量列表,即在上面的例子中,结果是:
url:
https://flyingcar.org/dfr/?d=34u&f=56&dossier=ZT18174&document=US1234567&docs='US1','US2','US3'#{'para1':'UK','para2':'Ireland'}
parsed.params=''
parsed.query="d=34u&f=56&dossier=ZT18174&document=US1234567&docs='US1','US2','US3'"
parsed.fragment="{'para1':'UK','para2':'Ireland'}"
parse_qs of querys: {'d': ['34u'], 'f': ['56'], 'dossier': ['ZT18174'], 'document': ['US1234567'], 'docs': ["'US1','US2','US3'"]} type: <class 'dict'>
here the parameters: {'para1':'UK','para2':'Ireland'} type: <class 'str'>
我希望在查询中,变量docs包含一个列表,而不是字符串,比如:
'docs': ['US1','US2','US3']
#instead of:
'docs': ["'US1','US2','US3'"]
我应该如何构建URL?
您可以在解析URL:后使用此片段
for i in captured_values:
captured_values[i]=captured_values[i][0].replace("'","").split(",")
print(captured_values)
输出:
{'d': ['34u'], 'f': ['56'], 'dossier': ['ZT18174'], 'document': ['US1234567'], 'docs': ['US1', 'US2', 'US3']}
虽然我觉得更换它不太好,但你可以使用解决方案