mirror of https://github.com/scrapy/scrapy.git
Document urlparse_cached (#7777)
This commit is contained in:
parent
41bb09741a
commit
58ed9fdccc
|
|
@ -342,6 +342,8 @@ Other functions related to requests
|
|||
|
||||
.. autofunction:: scrapy.utils.request.request_from_dict
|
||||
|
||||
.. autofunction:: scrapy.utils.httpobj.urlparse_cached
|
||||
|
||||
|
||||
.. _topics-request-response-ref-request-callback-arguments:
|
||||
|
||||
|
|
|
|||
|
|
@ -16,8 +16,15 @@ _urlparse_cache: WeakKeyDictionary[Request | Response, ParseResult] = (
|
|||
|
||||
|
||||
def urlparse_cached(request_or_response: Request | Response) -> ParseResult:
|
||||
"""Return urlparse.urlparse caching the result, where the argument can be a
|
||||
Request or Response object
|
||||
"""Return the result of parsing the URL of *request_or_response*, a
|
||||
:class:`~scrapy.Request` or :class:`~scrapy.http.Response` object, with
|
||||
:func:`urllib.parse.urlparse`.
|
||||
|
||||
The result is cached, using a :class:`weakref.WeakKeyDictionary` keyed on
|
||||
*request_or_response*, so that the URL of a given object is parsed only
|
||||
once. Prefer this function over calling :func:`urllib.parse.urlparse` on
|
||||
``request_or_response.url`` directly when the same URL may be parsed more
|
||||
than once.
|
||||
"""
|
||||
if request_or_response not in _urlparse_cache:
|
||||
_urlparse_cache[request_or_response] = urlparse(request_or_response.url)
|
||||
|
|
|
|||
Loading…
Reference in New Issue