例如,我有以下网址格式:
url_pattern = {
"scheme": "http",
"netloc": "for.example.com",
"path": "/ex1",
"params": "",
"query": a=b&c=d,
"fragment": ""
}
就像urlparse.urlparse("http://for.example.com/ex1?a=b&c=d")
输出的倒数一样。我可以从字典中获取网址吗?
答案 0 :(得分:2)
urlparse
生成ParseResult
个对象。因此,只需构造ParseResult
的对象并使用geturl()
方法生成URL。
>>> url_pattern = {
"scheme": "http",
"netloc": "for.example.com",
"path": "/ex1",
"params": "",
"query": "a=b&c=d",
"fragment": ""
}
>>> from urlparse import ParseResult
>>> ParseResult(**url_pattern).geturl()
'http://for.example.com/ex1?a=b&c=d'
答案 1 :(得分:0)
def geturl(pattern):
return pattern["scheme"] + "://" + pattern["netloc"] + pattern["path"] + ("?" if pattern["query"]) + pattern["query"]
应该运作良好。