代码之家  ›  专栏  ›  技术社区  ›  Dmitrii Mikhailov

如何在scrapy downloader中间件中获取响应体

  •  2
  • Dmitrii Mikhailov  · 技术社区  · 8 年前

    如果页面上没有找到某些XPath,我需要能够重试请求。所以我写了这个中间件:

    class ManualRetryMiddleware(RetryMiddleware):
        def process_response(self, request, response, spider):
            if not spider.retry_if_not_found:
                return response
            if not hasattr(response, 'text') and response.status != 200:
                return super(ManualRetryMiddleware, self).process_response(request, response, spider)
            found = False
            for xpath in spider.retry_if_not_found:
                if response.xpath(xpath).extract():
                    found = True
                    break
            if not found:
                return self._retry(request, "Didn't find anything useful", spider)
            return response
    

    并在 settings.py :

    DOWNLOADER_MIDDLEWARES = {
        'myproject.middlewares.ManualRetryMiddleware': 650,
        'scrapy.downloadermiddlewares.retry.RetryMiddleware': None,
    }
    

    当我运行蜘蛛时,我得到

    AttributeError: 'Response' object has no attribute 'xpath'
    

    text 财产和 response.body

    那么,我如何在中间件中检查页面内容呢?有些页面可能不包含我需要的详细信息,因此我希望能够稍后再试。

    2 回复  |  直到 8 年前
        1
  •  1
  •   TomáÅ¡ Linhart    8 年前

    response 不包含 xpath 方法是 process_response downloader中间件的方法为类型 scrapy.http.Response ,请参阅 documentation scrapy.http.TextResponse scrapy.http.HtmlResponse xpath 方法所以在使用之前 xpath 创造 HtmlResponse 对象来自 . 课堂上相应的部分将成为:

    ...
    new_response = scrapy.http.HtmlResponse(response.url, body=response.body)
    if new_response.xpath(xpath).extract():
        found = True
        break
    ...
    
        2
  •  1
  •   mouch    8 年前

    还要注意你的中间件位置。它需要在 scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware Content-Encoding: gzip .