Python scrapy.http.Response() Examples
The following are 30
code examples of scrapy.http.Response().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
scrapy.http
, or try the search function
.
Example #1
Source File: iterators.py From learn_python3_spider with MIT License | 6 votes |
def _body_or_str(obj, unicode=True): expected_types = (Response, six.text_type, six.binary_type) assert isinstance(obj, expected_types), \ "obj must be %s, not %s" % ( " or ".join(t.__name__ for t in expected_types), type(obj).__name__) if isinstance(obj, Response): if not unicode: return obj.body elif isinstance(obj, TextResponse): return obj.text else: return obj.body.decode('utf-8') elif isinstance(obj, six.text_type): return obj if unicode else obj.encode('utf-8') else: return obj.decode('utf-8') if unicode else obj
Example #2
Source File: iterators.py From learn_python3_spider with MIT License | 6 votes |
def xmliter(obj, nodename): """Return a iterator of Selector's over all nodes of a XML document, given the name of the node to iterate. Useful for parsing XML feeds. obj can be: - a Response object - a unicode string - a string encoded as utf-8 """ nodename_patt = re.escape(nodename) HEADER_START_RE = re.compile(r'^(.*?)<\s*%s(?:\s|>)' % nodename_patt, re.S) HEADER_END_RE = re.compile(r'<\s*/%s\s*>' % nodename_patt, re.S) text = _body_or_str(obj) header_start = re.search(HEADER_START_RE, text) header_start = header_start.group(1).strip() if header_start else '' header_end = re_rsearch(HEADER_END_RE, text) header_end = text[header_end[1]:].strip() if header_end else '' r = re.compile(r'<%(np)s[\s>].*?</%(np)s>' % {'np': nodename_patt}, re.DOTALL) for match in r.finditer(text): nodetext = header_start + match.group() + header_end yield Selector(text=nodetext, type='xml').xpath('//' + nodename)[0]
Example #3
Source File: iterators.py From learn_python3_spider with MIT License | 6 votes |
def _body_or_str(obj, unicode=True): expected_types = (Response, six.text_type, six.binary_type) assert isinstance(obj, expected_types), \ "obj must be %s, not %s" % ( " or ".join(t.__name__ for t in expected_types), type(obj).__name__) if isinstance(obj, Response): if not unicode: return obj.body elif isinstance(obj, TextResponse): return obj.text else: return obj.body.decode('utf-8') elif isinstance(obj, six.text_type): return obj if unicode else obj.encode('utf-8') else: return obj.decode('utf-8') if unicode else obj
Example #4
Source File: referer.py From learn_python3_spider with MIT License | 6 votes |
def policy(self, resp_or_url, request): """ Determine Referrer-Policy to use from a parent Response (or URL), and a Request to be sent. - if a valid policy is set in Request meta, it is used. - if the policy is set in meta but is wrong (e.g. a typo error), the policy from settings is used - if the policy is not set in Request meta, but there is a Referrer-policy header in the parent response, it is used if valid - otherwise, the policy from settings is used. """ policy_name = request.meta.get('referrer_policy') if policy_name is None: if isinstance(resp_or_url, Response): policy_header = resp_or_url.headers.get('Referrer-Policy') if policy_header is not None: policy_name = to_native_str(policy_header.decode('latin1')) if policy_name is None: return self.default_policy() cls = _load_policy_class(policy_name, warning_only=True) return cls() if cls else self.default_policy()
Example #5
Source File: test_retry_middleware.py From scrapy-fake-useragent with BSD 3-Clause "New" or "Revised" License | 6 votes |
def retry_middleware_response(request): """ Fixture to simplify creating a crawler with an activated middleware and going through the request-response cycle. Executes process_response() method of the middleware. """ settings, status = request.param crawler = get_crawler(Spider, settings_dict=settings) spider = crawler._create_spider('foo') mw = RetryUserAgentMiddleware.from_crawler(crawler) req = Request('http://www.scrapytest.org/') rsp = Response(req.url, body=b'', status=status) yield mw.process_response(req, rsp, spider)
Example #6
Source File: httpcompression.py From learn_python3_spider with MIT License | 6 votes |
def process_response(self, request, response, spider): if request.method == 'HEAD': return response if isinstance(response, Response): content_encoding = response.headers.getlist('Content-Encoding') if content_encoding: encoding = content_encoding.pop() decoded_body = self._decode(response.body, encoding.lower()) respcls = responsetypes.from_args(headers=response.headers, \ url=response.url, body=decoded_body) kwargs = dict(cls=respcls, body=decoded_body) if issubclass(respcls, TextResponse): # force recalculating the encoding until we make sure the # responsetypes guessing is reliable kwargs['encoding'] = None response = response.replace(**kwargs) if not content_encoding: del response.headers['Content-Encoding'] return response
Example #7
Source File: engine.py From learn_python3_spider with MIT License | 6 votes |
def _download(self, request, spider): slot = self.slot slot.add_request(request) def _on_success(response): assert isinstance(response, (Response, Request)) if isinstance(response, Response): response.request = request # tie request to response received logkws = self.logformatter.crawled(request, response, spider) logger.log(*logformatter_adapter(logkws), extra={'spider': spider}) self.signals.send_catch_log(signal=signals.response_received, \ response=response, request=request, spider=spider) return response def _on_complete(_): slot.nextcall.schedule() return _ dwld = self.downloader.fetch(request, spider) dwld.addCallbacks(_on_success) dwld.addBoth(_on_complete) return dwld
Example #8
Source File: httpcompression.py From learn_python3_spider with MIT License | 6 votes |
def process_response(self, request, response, spider): if request.method == 'HEAD': return response if isinstance(response, Response): content_encoding = response.headers.getlist('Content-Encoding') if content_encoding: encoding = content_encoding.pop() decoded_body = self._decode(response.body, encoding.lower()) respcls = responsetypes.from_args(headers=response.headers, \ url=response.url, body=decoded_body) kwargs = dict(cls=respcls, body=decoded_body) if issubclass(respcls, TextResponse): # force recalculating the encoding until we make sure the # responsetypes guessing is reliable kwargs['encoding'] = None response = response.replace(**kwargs) if not content_encoding: del response.headers['Content-Encoding'] return response
Example #9
Source File: engine.py From learn_python3_spider with MIT License | 6 votes |
def _download(self, request, spider): slot = self.slot slot.add_request(request) def _on_success(response): assert isinstance(response, (Response, Request)) if isinstance(response, Response): response.request = request # tie request to response received logkws = self.logformatter.crawled(request, response, spider) logger.log(*logformatter_adapter(logkws), extra={'spider': spider}) self.signals.send_catch_log(signal=signals.response_received, \ response=response, request=request, spider=spider) return response def _on_complete(_): slot.nextcall.schedule() return _ dwld = self.downloader.fetch(request, spider) dwld.addCallbacks(_on_success) dwld.addBoth(_on_complete) return dwld
Example #10
Source File: iterators.py From learn_python3_spider with MIT License | 6 votes |
def xmliter(obj, nodename): """Return a iterator of Selector's over all nodes of a XML document, given the name of the node to iterate. Useful for parsing XML feeds. obj can be: - a Response object - a unicode string - a string encoded as utf-8 """ nodename_patt = re.escape(nodename) HEADER_START_RE = re.compile(r'^(.*?)<\s*%s(?:\s|>)' % nodename_patt, re.S) HEADER_END_RE = re.compile(r'<\s*/%s\s*>' % nodename_patt, re.S) text = _body_or_str(obj) header_start = re.search(HEADER_START_RE, text) header_start = header_start.group(1).strip() if header_start else '' header_end = re_rsearch(HEADER_END_RE, text) header_end = text[header_end[1]:].strip() if header_end else '' r = re.compile(r'<%(np)s[\s>].*?</%(np)s>' % {'np': nodename_patt}, re.DOTALL) for match in r.finditer(text): nodetext = header_start + match.group() + header_end yield Selector(text=nodetext, type='xml').xpath('//' + nodename)[0]
Example #11
Source File: serialize.py From learn_python3_spider with MIT License | 6 votes |
def default(self, o): if isinstance(o, set): return list(o) elif isinstance(o, datetime.datetime): return o.strftime("%s %s" % (self.DATE_FORMAT, self.TIME_FORMAT)) elif isinstance(o, datetime.date): return o.strftime(self.DATE_FORMAT) elif isinstance(o, datetime.time): return o.strftime(self.TIME_FORMAT) elif isinstance(o, decimal.Decimal): return str(o) elif isinstance(o, defer.Deferred): return str(o) elif isinstance(o, BaseItem): return dict(o) elif isinstance(o, Request): return "<%s %s %s>" % (type(o).__name__, o.method, o.url) elif isinstance(o, Response): return "<%s %s %s>" % (type(o).__name__, o.status, o.url) else: return super(ScrapyJSONEncoder, self).default(o)
Example #12
Source File: __init__.py From scrapy-wayback-machine with ISC License | 6 votes |
def process_response(self, request, response, spider): meta = request.meta # parse CDX requests and schedule future snapshot requests if meta.get('wayback_machine_cdx_request'): snapshot_requests = self.build_snapshot_requests(response, meta) # treat empty listings as 404s if len(snapshot_requests) < 1: return Response(meta['wayback_machine_original_request'].url, status=404) # schedule all of the snapshots for snapshot_request in snapshot_requests: self.crawler.engine.schedule(snapshot_request, spider) # abort this request raise UnhandledIgnoreRequest # clean up snapshot responses if meta.get('wayback_machine_url'): return response.replace(url=meta['wayback_machine_original_request'].url) return response
Example #13
Source File: spiders.py From zulip with Apache License 2.0 | 6 votes |
def _vnu_callback(self, url: str) -> Callable[[Response], None]: def callback(response: Response) -> None: vnu_out = json.loads(response.text) for message in vnu_out['messages']: if not VNU_IGNORE.fullmatch(message['message']): self.logger.error( '"%s":%d.%d-%d.%d: %s: %s', url, message.get('firstLine', message['lastLine']), message.get('firstColumn', message['lastColumn']), message['lastLine'], message['lastColumn'], message['type'], message['message'], ) return callback
Example #14
Source File: spiders.py From zulip with Apache License 2.0 | 6 votes |
def parse(self, response: Response) -> Iterator[Request]: self.log(response) if getattr(self, 'validate_html', False): yield Request( 'http://127.0.0.1:9988/?out=json', method='POST', headers={'Content-Type': response.headers['Content-Type']}, body=response.body, callback=self._vnu_callback(response.url), errback=self.error_callback, ) for link in LxmlLinkExtractor(deny_domains=self.deny_domains, deny_extensions=['doc'], tags=self.tags, attrs=self.attrs, deny=self.deny, canonicalize=False).extract_links(response): yield from self._make_requests(link.url)
Example #15
Source File: middleware.py From scrapy-poet with BSD 3-Clause "New" or "Revised" License | 6 votes |
def process_request(self, request: Request, spider: Spider): """This method checks if the request is really needed and if its download could be skipped by trying to infer if a ``Response`` is going to be used by the callback or a Page Input. If the ``Response`` can be ignored, a ``utils.DummyResponse`` object is returned on its place. This ``DummyResponse`` is linked to the original ``Request`` instance. With this behavior, we're able to optimize spider executions avoiding unnecessary downloads. That could be the case when the callback is actually using another source like external APIs such as Scrapinghub's Auto Extract. """ if utils.is_response_going_to_be_used(request, spider): return spider.logger.debug(f'Skipping download of {request}') return utils.DummyResponse(url=request.url, request=request)
Example #16
Source File: test_autoextract.py From scrapy-autoextract with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _assert_enabled(spider, settings=None, url='http://quotes.toscrape.com', api_url='autoextract.scrapinghub.com', api_auth=basic_auth_header('apikey', '')): mw = _mock_mw(spider, settings) req = Request(url, meta=AUTOX_META) out = mw.process_request(req, spider) assert api_url in out.url assert out.meta['autoextract'].get('enabled') assert out.headers.get('Authorization') == api_auth assert 'User-Agent' in out.headers resp = Response(out.url, request=out, body=b'[{}]') proc = mw.process_response(out, resp, spider) assert proc.meta['autoextract'].get('original_url') == url assert isinstance(proc.meta['autoextract'].get('article'), dict)
Example #17
Source File: responsetypes.py From learn_python3_spider with MIT License | 5 votes |
def from_args(self, headers=None, url=None, filename=None, body=None): """Guess the most appropriate Response class based on the given arguments.""" cls = Response if headers is not None: cls = self.from_headers(headers) if cls is Response and url is not None: cls = self.from_filename(url) if cls is Response and filename is not None: cls = self.from_filename(filename) if cls is Response and body is not None: cls = self.from_body(body) return cls
Example #18
Source File: test_policy.py From scrapy-rotating-proxies with MIT License | 5 votes |
def get_response(**kwargs): return Response(request.url, request=request, **kwargs)
Example #19
Source File: tests.py From daywatch with MIT License | 5 votes |
def resp(text): return http.Response(url='http://tryolabs.com', body=text)
Example #20
Source File: test_autoextract.py From scrapy-autoextract with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_request_error(): mw = _mock_mw(spider, MW_SETTINGS) req = Request('http://quotes.toscrape.com', meta=AUTOX_META) out = mw.process_request(req, spider) err = b'{"title":"No authentication token provided","type":"http://errors.xod.scrapinghub.com/unauthorized.html"}' resp = Response(out.url, request=out, body=err) with pytest.raises(AutoExtractError): mw.process_response(out, resp, spider)
Example #21
Source File: httpcache.py From learn_python3_spider with MIT License | 5 votes |
def should_cache_response(self, response, request): # What is cacheable - https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1 # Response cacheability - https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.4 # Status code 206 is not included because cache can not deal with partial contents cc = self._parse_cachecontrol(response) # obey directive "Cache-Control: no-store" if b'no-store' in cc: return False # Never cache 304 (Not Modified) responses elif response.status == 304: return False # Cache unconditionally if configured to do so elif self.always_store: return True # Any hint on response expiration is good elif b'max-age' in cc or b'Expires' in response.headers: return True # Firefox fallbacks this statuses to one year expiration if none is set elif response.status in (300, 301, 308): return True # Other statuses without expiration requires at least one validator elif response.status in (200, 203, 401): return b'Last-Modified' in response.headers or b'ETag' in response.headers # Any other is probably not eligible for caching # Makes no sense to cache responses that does not contain expiration # info and can not be revalidated else: return False
Example #22
Source File: cookies.py From learn_python3_spider with MIT License | 5 votes |
def _get_request_cookies(self, jar, request): if isinstance(request.cookies, dict): cookie_list = [{'name': k, 'value': v} for k, v in \ six.iteritems(request.cookies)] else: cookie_list = request.cookies cookies = [self._format_cookie(x) for x in cookie_list] headers = {'Set-Cookie': cookies} response = Response(request.url, headers=headers) return jar.make_cookies(response, request)
Example #23
Source File: responsetypes.py From learn_python3_spider with MIT License | 5 votes |
def from_mimetype(self, mimetype): """Return the most appropriate Response class for the given mimetype""" if mimetype is None: return Response elif mimetype in self.classes: return self.classes[mimetype] else: basetype = "%s/*" % mimetype.split('/')[0] return self.classes.get(basetype, Response)
Example #24
Source File: responsetypes.py From learn_python3_spider with MIT License | 5 votes |
def from_content_type(self, content_type, content_encoding=None): """Return the most appropriate Response class from an HTTP Content-Type header """ if content_encoding: return Response mimetype = to_native_str(content_type).split(';')[0].strip().lower() return self.from_mimetype(mimetype)
Example #25
Source File: responsetypes.py From learn_python3_spider with MIT License | 5 votes |
def from_filename(self, filename): """Return the most appropriate Response class from a file name""" mimetype, encoding = self.mimetypes.guess_type(filename) if mimetype and not encoding: return self.from_mimetype(mimetype) else: return Response
Example #26
Source File: utils.py From scrapy-autounit with BSD 3-Clause "New" or "Revised" License | 5 votes |
def parse_object(_object, spider): if isinstance(_object, Request): return parse_request(_object, spider) elif isinstance(_object, Response): return parse_object(response_to_dict(_object), spider) elif isinstance(_object, dict): for k, v in _object.items(): _object[k] = parse_object(v, spider) elif isinstance(_object, (list, tuple)): for i, v in enumerate(_object): _object[i] = parse_object(v, spider) return _object
Example #27
Source File: engine.py From learn_python3_spider with MIT License | 5 votes |
def _handle_downloader_output(self, response, request, spider): assert isinstance(response, (Request, Response, Failure)), response # downloader middleware can return requests (for example, redirects) if isinstance(response, Request): self.crawl(response, spider) return # response is a Response or Failure d = self.scraper.enqueue_scrape(response, request, spider) d.addErrback(lambda f: logger.error('Error while enqueuing downloader output', exc_info=failure_to_exc_info(f), extra={'spider': spider})) return d
Example #28
Source File: test_autoextract.py From scrapy-autoextract with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _assert_disabled(spider, settings=None): mw = _mock_mw(spider, settings) req = Request('http://quotes.toscrape.com', meta=AUTOX_META) out = mw.process_request(req, spider) assert out is None assert req.meta.get('autoextract') is None res = Response(req.url, request=req) assert mw.process_response(req, res, spider) == res
Example #29
Source File: scraper.py From learn_python3_spider with MIT License | 5 votes |
def add_response_request(self, response, request): deferred = defer.Deferred() self.queue.append((response, request, deferred)) if isinstance(response, Response): self.active_size += max(len(response.body), self.MIN_RESPONSE_SIZE) else: self.active_size += self.MIN_RESPONSE_SIZE return deferred
Example #30
Source File: scraper.py From learn_python3_spider with MIT License | 5 votes |
def finish_response(self, response, request): self.active.remove(request) if isinstance(response, Response): self.active_size -= max(len(response.body), self.MIN_RESPONSE_SIZE) else: self.active_size -= self.MIN_RESPONSE_SIZE