我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用urllib.parse.urldefrag()。
def get(self): url = request.args.get('url') tags = request.args.getlist('tag') filters = [db.Bookmark.user == current_user.id] if url is not None: filters.append(db.Bookmark.url == urldefrag(url).url) filters.append(db.Bookmark.tags.contains(tags)) result = db.Bookmark.query.filter(*filters) \ .order_by( db.Bookmark.read.desc().nullsfirst(), db.Bookmark.timestamp.desc()) \ .paginate() headers = {} links = [] if result.has_next: last_url = update_query(request.url, {'page': result.pages}) links.append(lh.Link(last_url, rel='last')) if links: headers['Link'] = lh.format_links(links) return list(map(lambda x: x.to_dict(), result.items)), 200, headers
def removeURLHash(url: str) -> str: """Remove url hash.""" urlObject, _ = urldefrag(url) return urlunparse(urlObject)
def remove_fragment(url): pure_url, _ = urldefrag(url) return pure_url
def post(self, id): update = request.get_json() if 'id' in update: return {'error': 'Updating id is not allowed'}, \ HTTPStatus.BAD_REQUEST bookmark = db.Bookmark.query \ .filter_by(id=id, user=current_user.id) \ .first() if bookmark is None: return {'error': 'Not found'}, HTTPStatus.NOT_FOUND if 'url' in update: bookmark.url = urldefrag(update['url']).url if 'title' in update: bookmark.title = update['title'] if 'timestamp' in update: bookmark.timestamp = aniso8601.parse_datetime(update['timestamp']) if 'read' in update: if update['read']: bookmark.read = aniso8601.parse_datetime(update['read']) else: bookmark.read = None db.db.session.add(bookmark) db.db.session.commit() return bookmark.to_dict(), HTTPStatus.OK
def remove_fragment(url): pure_url, frag = urldefrag(url) # ??#? return pure_url
def remove_fragment(url): pure_url, frag = urldefrag(url) return pure_url
def _urljoin(base, url): """ Construct a full ("absolute") URL by combining a "base URL" with another URL. Informally, this uses components of the base URL, in particular the addressing scheme, the network location and (part of) the path, to provide missing components in the relative URL. Additionally, the fragment identifier is preserved according to the HTTP 1.1 bis draft. @type base: C{bytes} @param base: Base URL. @type url: C{bytes} @param url: URL to combine with C{base}. @return: An absolute URL resulting from the combination of C{base} and C{url}. @see: L{urlparse.urljoin} @see: U{https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-22#section-7.1.2} """ base, baseFrag = urldefrag(base) url, urlFrag = urldefrag(urljoin(base, url)) return urljoin(url, b'#' + (urlFrag or baseFrag))
def post(self): r = request.get_json() if not r: return {'error': 'payload is mandatory'}, HTTPStatus.BAD_REQUEST if isinstance(r, dict): r = [r] bookmarks = [] now = datetime.datetime.utcnow().isoformat() for entity in r: if 'url' not in entity: return {'error': 'url field is mandatory'}, \ HTTPStatus.BAD_REQUEST url = urldefrag(entity['url']).url if entity.get('read'): read = aniso8601.parse_datetime(entity.get('read')) else: read = None if entity.get('title'): title = entity.get('title') else: title = entity.get('url') tags = entity.get('tags') bookmark = db.Bookmark( user=current_user.id, url=url, title=title, timestamp=aniso8601.parse_datetime( entity.get('timestamp', now)), read=read, tags=tags) db.db.session.add(bookmark) db.db.session.commit() bookmarks.append(bookmark) q.enqueue(article.fetch_article, bookmark.id, url) res = list(map(lambda x: x.to_dict(), bookmarks)) return res[0] if len(res) == 1 else res, HTTPStatus.CREATED
def goto_url(request): """ View that takes the user to a URL with the annotation layer enabled. Optional configuration for the client may be specified via additional query params: "q" - Initial query for the filter input in the client. """ settings = request.registry.settings url = request.params.get('url') if url is None: raise httpexceptions.HTTPBadRequest('"url" parameter is missing') if not _is_valid_http_url(url): raise httpexceptions.HTTPBadRequest( _('Sorry, but this service can only show annotations on ' 'valid HTTP or HTTPs URLs.')) # Remove any existing #fragment identifier from the URI before we # append our own. url = parse.urldefrag(url)[0] query = parse.quote(request.params.get('q', '')) via_url = '{via_base_url}/{url}#annotations:query:{query}'.format( via_base_url=settings['via_base_url'], url=url, query=query) extension_url = '{url}#annotations:query:{query}'.format( url=url, query=query) pretty_url = util.get_pretty_url(url) return { 'data': json.dumps({ 'chromeExtensionId': settings['chrome_extension_id'], 'viaUrl': via_url, 'extensionUrl': extension_url, }), 'pretty_url': pretty_url }