我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用urlparse.quote()。
def test_urlencode_quote_via(self): result = urlparse.urlencode({'a': 'some value'}) self.assertEqual(result, "a=some+value") result = urlparse.urlencode({'a': 'some value/another'}, quote_via=urlparse.quote) self.assertEqual(result, "a=some%20value%2Fanother") result = urlparse.urlencode({'a': 'some value/another'}, safe='/', quote_via=urlparse.quote) self.assertEqual(result, "a=some%20value/another")
def test_quote_errors(self): self.assertRaises(TypeError, urlparse.quote, b'foo', encoding='utf-8') self.assertRaises(TypeError, urlparse.quote, b'foo', errors='strict')
def download(url, out=None, bar=bar_adaptive): """High level function, which downloads URL into tmp file in current directory and then renames it to filename autodetected from either URL or HTTP headers. :param bar: function to track download progress (visualize etc.) :param out: output filename or directory :return: filename where URL is downloaded to """ # detect of out is a directory outdir = None if out and os.path.isdir(out): outdir = out out = None # get filename for temp file in current directory prefix = detect_filename(url, out) (fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=prefix, dir=".") os.close(fd) os.unlink(tmpfile) # set progress monitoring callback def callback_charged(blocks, block_size, total_size): # 'closure' to set bar drawing function in callback callback_progress(blocks, block_size, total_size, bar_function=bar) if bar: callback = callback_charged else: callback = None if PY3K: # Python 3 can not quote URL as needed binurl = list(urlparse.urlsplit(url)) binurl[2] = urlparse.quote(binurl[2]) binurl = urlparse.urlunsplit(binurl) else: binurl = url (tmpfile, headers) = ulib.urlretrieve(binurl, tmpfile, callback) filename = detect_filename(url, out, headers) if outdir: filename = outdir + "/" + filename # add numeric ' (x)' suffix if filename already exists if os.path.exists(filename): filename = filename_fix_existing(filename) shutil.move(tmpfile, filename) # print headers return filename