我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用urllib.parse.ParseResult()。
def prepare(self): parser = urlparse(self.url) scheme = parser.scheme host = parser.netloc path = parser.path _params = parser.params query = parser.query fragment = parser.fragment if self.params: enc_params = url_params_encode(self.params) query = '{}&{}'.format(query, enc_params) self.url_parse_result = ParseResult( scheme = scheme or 'http', netloc = host, path = path, params = _params, query = query, fragment = fragment) self.ssl = scheme.lower() == 'https'
def add_params(url, **params): """ Append or replace query params in `url` using `params`. """ url = parse.unquote(url) parsed = parse.urlparse(url) existing = dict(parse.parse_qsl(parsed.query)) existing.update(params) return parse.urlunparse( parse.ParseResult( scheme=parsed.scheme, netloc=parsed.netloc, path=parsed.path, params=parsed.params, query=parse.urlencode(existing), fragment=parsed.fragment ) )
def alter_url(url, **components): """ >>> alter_url('http://placekitten.com', path='200/300') 'http://placekitten.com/200/300' """ original = urlparse(url)._asdict() original.update(components) return ParseResult(**original).geturl()
def __init__(self, dsn: ParseResult, *, loop: asyncio.AbstractEventLoop = None): """ :param dsn: The :class:`urllib.parse.ParseResult` created from parsing a DSN. """ self.loop = loop or asyncio.get_event_loop() self._parse_result = dsn self.dsn = dsn.geturl() self.host = dsn.hostname self.port = dsn.port self.username = dsn.username self.password = dsn.password self.db = dsn.path[1:] self.params = {k: v[0] for k, v in parse_qs(dsn.query).items()}
def build_url(obj): scheme = getattr(obj, 'scheme', 'https') port = getattr(obj, 'port', None) # skip useless port specification if (not port or scheme == 'https' and str(port) == '443' or scheme == 'http' and str(port) == '80'): netloc = obj.fqdn else: netloc = '{}:{}'.format(obj.fqdn, port) return ParseResult(scheme, netloc, *['']*4).geturl()
def safeUrl(url:str)->str: from urllib import parse pr = parse.urlparse(url) pr2 = parse.ParseResult(scheme=pr.scheme, netloc=pr.netloc, path=parse.quote(pr.path,'/%'), params=pr.params, query=pr.query, fragment=pr.fragment) return parse.urlunparse(pr2)
def test_marshal_uri(self): """ Should parse a 'uri' value into a `ParseResult`. """ uri = 'https://media.giphy.com/media/22kxQ12cxyEww/giphy.gif?something=variable' marshalled, val = upnp.marshal.marshal_value('uri', uri) self.assertTrue(marshalled) self.assertIsInstance(val, ParseResult)
def parse_url(url): url = urlparse.urlparse(url) if not url.scheme and not url.netloc and url.path: netloc = url.path if ':' not in netloc: netloc = '{}:8765'.format(netloc) return urlparse.ParseResult('http', netloc, '/', '', '', '') return url # Defined in phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java
def __to_url(url): if isinstance(url, __urlparse.ParseResult): return url return __urlparse.urlparse(url)
def urlparams(url_, fragment=None, query_dict=None, **query): """ Add a fragment and/or query parameters to a URL. New query params will be appended to exising parameters, except duplicate names, which will be replaced. """ url_ = urlparse.urlparse(url_) fragment = fragment if fragment is not None else url_.fragment q = url_.query new_query_dict = (QueryDict(smart_str(q), mutable=True) if q else QueryDict('', mutable=True)) if query_dict: for k, l in query_dict.lists(): new_query_dict[k] = None # Replace, don't append. for v in l: new_query_dict.appendlist(k, v) for k, v in query.items(): # Replace, don't append. if isinstance(v, list): new_query_dict.setlist(k, v) else: new_query_dict[k] = v query_string = urlencode([(k, v) for k, l in new_query_dict.lists() for v in l if v is not None]) new = urlparse.ParseResult(url_.scheme, url_.netloc, url_.path, url_.params, query_string, fragment) return new.geturl()
def make_support_link(email, appname='SLiM'): support_link_parts = ParseResult( scheme='mailto', netloc='', path=email, params='', query='subject=' + quote('[%s] Support request' % appname), # query=urlencode({'subject': '[%s] Support request' % appname}, # quote_via=quote), fragment='', ) return urlunparse(support_link_parts)
def sqlite_uri_for(path): uri_parts = ParseResult( scheme='sqlite', netloc='/', path=path, params='', query='', fragment='') return urlunparse(uri_parts)
def __init__(self, url): if isinstance(url, ParseResult) or isinstance(url, SplitResult): self.url_parsed = url self.url_raw = None else: self.url_raw = url self.url_parsed = None
def objectify(self, url): if url is None: return None if isinstance(url, URL): return url else: return URL(url) # To deal with all kind of methods/properties in the ParseResult # class
def unauth(self): if not self.is_auth(): return self return URL.objectify(ParseResult( self.scheme, '%s:%s' % (self.hostname, self.port or {'https': 443, 'http': 80}[self.scheme]), self.path.replace('//', '/'), self.params, self.query, self.fragment))
def join(self, path): """ assumes this object is the base URL or base path. If the path is relative, it should be appended to the base. If the path is absolute, it should be added to the connection details of self. If the path already contains connection details and the connection details differ from self, raise an error. """ pathAsString = str(path) if not path or not pathAsString: return self path = URL.objectify(path) if ( (path.scheme and self.scheme and path.scheme != self.scheme) or (path.hostname and self.hostname and path.hostname != self.hostname) or (path.port and self.port and path.port != self.port) ): raise ValueError("%s can't be joined with %s" % (self, path)) if path.path[0] == '/': ret_path = uc2utf8(path.path) else: sep = "/" if self.path.endswith("/"): sep = "" ret_path = "%s%s%s" % (self.path, sep, uc2utf8(path.path)) return URL(ParseResult( self.scheme or path.scheme, self.netloc or path.netloc, ret_path, path.params, path.query, path.fragment))
def test_url_cast(self, set_env, env): set_env({'URL': 'http://stevenloria.com/projects/?foo=42'}) res = env.url('URL') assert isinstance(res, urlparse.ParseResult)