下面是一個簡單的類,使您能夠做到這一點:
if Url(url1) == Url(url2):
pass
它很容易被改組爲一個功能,雖然這些對象是可哈希的,因此使您能夠使用一組或字典,將它們添加到緩存:
from urlparse import urlparse, parse_qsl
from urllib import unquote_plus
class Url(object):
'''A url object that can be compared with other url orbjects
without regard to the vagaries of encoding, escaping, and ordering
of parameters in query strings.'''
def __init__(self, url):
parts = urlparse(url)
_query = frozenset(parse_qsl(parts.query))
_path = unquote_plus(parts.path)
parts = parts._replace(query=_query, path=_path)
self.parts = parts
def __eq__(self, other):
return self.parts == other.parts
def __hash__(self):
return hash(self.parts)
雖然主機名稱相同,但由於HTTP默認爲端口80,查詢字符串完全不同。 – 2011-03-20 22:31:28
重新排序查詢參數不一定會導致相同的資源。訂單是可觀察的,並且對於某些(pathalogical)服務可能很明顯。 – SingleNegationElimination 2011-03-20 23:15:19