Scrapy源码 Response对象
Scrapy源码 Response对象
"""This module implements the Response class which is used to represent HTTPresponses in Scrapy.See documentation in docs/topics/request-response.rst"""from six.moves.urllib.parse import urljoinfrom scrapy.http.request import Requestfrom scrapy.http.headers import Headersfrom scrapy.link import Linkfrom scrapy.utils.trackref import object_reffrom scrapy.http.common import obsolete_setterfrom scrapy.exceptions import NotSupportedclass Response(object_ref):def __init__(self, url, status=200, headers=None, body=b‘‘, flags=None, request=None):self.headers = Headers(headers or {})self.status = int(status)self._set_body(body)self._set_url(url)self.request = requestself.flags = [] if flags is None else list(flags)@property def meta(self):try:return self.request.metaexcept AttributeError:raise AttributeError("Response.meta not available, this response " "is not tied to any request" )def _get_url(self):return self._urldef _set_url(self, url):if isinstance(url, str):self._url = urlelse:raise TypeError(‘%s url must be str, got %s:‘ % (type(self).__name__, type(url).__name__)) url = property(_get_url, obsolete_setter(_set_url, ‘url‘))def _get_body(self):return self._bodydef _set_body(self, body):if body is None:self._body = b‘‘ elif not isinstance(body, bytes):raise TypeError("Response body must be bytes. " "If you want to pass unicode body use TextResponse " "or HtmlResponse.")else:self._body = body body = property(_get_body, obsolete_setter(_set_body, ‘body‘))def __str__(self):return "<%d %s>" % (self.status, self.url)__repr__ = __str__ def copy(self):"""Return a copy of this Response""" return self.replace()def replace(self, *args, **kwargs):"""Create a new Response with the same attributes except for those given new values. """ for x in [‘url‘, ‘status‘, ‘headers‘, ‘body‘, ‘request‘, ‘flags‘]: kwargs.setdefault(x, getattr(self, x)) cls = kwargs.pop(‘cls‘, self.__class__)return cls(*args, **kwargs)def urljoin(self, url):"""Join this Response‘s url with a possible relative url to form an absolute interpretation of the latter.""" return urljoin(self.url, url)@property def text(self):"""For subclasses of TextResponse, this will return the body as text (unicode object in Python 2 and str in Python 3) """ raise AttributeError("Response content isn‘t text")def css(self, *a, **kw):"""Shortcut method implemented only by responses whose content is text (subclasses of TextResponse). """ raise NotSupported("Response content isn‘t text")def xpath(self, *a, **kw):"""Shortcut method implemented only by responses whose content is text (subclasses of TextResponse). """ raise NotSupported("Response content isn‘t text")def follow(self, url, callback=None, method=‘GET‘, headers=None, body=None, cookies=None, meta=None, encoding=‘utf-8‘, priority=0, dont_filter=False, errback=None, cb_kwargs=None):# type: (...) -> Request """ Return a :class:`~.Request` instance to follow a link ``url``. It accepts the same arguments as ``Request.__init__`` method, but ``url`` can be a relative URL or a ``scrapy.link.Link`` object, not only an absolute URL. :class:`~.TextResponse` provides a :meth:`~.TextResponse.follow` method which supports selectors in addition to absolute/relative URLs and Link objects. """ if isinstance(url, Link): url = url.urlelif url
相关推荐
瓜牛呱呱 2020-11-12
柳木木的IT 2020-11-04
yifouhu 2020-11-02
lei0 2020-11-02
源码zanqunet 2020-10-28
源码zanqunet 2020-10-26
一叶梧桐 2020-10-14
码代码的陈同学 2020-10-14
lukezhong 2020-10-14
lzzyok 2020-10-10
anchongnanzi 2020-09-21
clh0 2020-09-18
changcongying 2020-09-17
星辰大海的路上 2020-09-13
abfdada 2020-08-26
mzy000 2020-08-24
shenlanse 2020-08-18
zhujiangtaotaise 2020-08-18
xiemanR 2020-08-17