def add_fragment(url, args): """ Add hash URL fragment in the most correct way by replacing the current one (if any). """ chunks = list(urlparse(url)) chunks[5] = urlencode(args) return urlunparse(chunks)
def add_arguments(url, args): """ Add GET arguments to the URL in the most correct way For example .. code-block:: python >>> add_arguments('http://example.com/foo.php?1=2', [(3, 4)]) 'http://example.com/foo.php?1=2&3=4' """ chunks = list(urlparse(url)) qs = parse_qsl(chunks[4]) qs += args chunks[4] = urlencode(qs) return urlunparse(chunks)