繁体   English   中英

在python中解析url以提取参数

[英]Parsing url in python to extract parameters

我在 python 中有一个 URL,比如

localhost:8080&id=123&id=345&name=john

有没有一个库可以解析这个并获取所有参数的键值对? 因为我想再次创建 url 作为使用 say id feild 的初始参数的组合,所以我会有两个新的 URL。

localhost:8080&id=123&name=johnlocalhost:8080&id=345&name=john

我在 python 2.7 中使用了这段代码,但它没有同时带来两个 ID

parse_url = urlparse(url)
query_dict = dict(parse_qsl(parse_url.query))
print query_dict

我想你可以用正则表达式来完成这个

import re
components = [re.split(":|=", component) for component in <url>.split("&")]
url_dict = {component[0]: component[1] for component in components}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM