如何在垃圾下载程序mid中获取响应体

2024-10-01 04:51:32 发布

您现在位置:Python中文网/ 问答频道 /正文

如果页面上没有找到某些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中注册:

^{pr2}$

当我跑蜘蛛的时候

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

我试图手动创建选择器并在其上运行xpath。。。但是响应没有text属性,response.body是字节,而不是str。。。在

那么如何在中间件中检查页面内容呢?有可能有些页面不包含我需要的详细信息,所以我想稍后再试一次。在


Tags: 中间件textselfreturnifresponserequestnot
2条回答

还要注意中间件的位置。它必须在scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware之前,否则,您可能会尝试解码压缩数据(这确实不起作用)。检查响应.标题要知道响应是否被压缩-Content-Encoding: gzip。在

response不包含xpath方法的原因是下载中间件的process_response方法中的response参数属于{a1}类型,参见documentation。只有^{}(和^{})有xpath方法。所以在使用xpath之前,从response创建{}对象。相应的部分将变成:

...
new_response = scrapy.http.HtmlResponse(response.url, body=response.body)
if new_response.xpath(xpath).extract():
    found = True
    break
...

相关问题 更多 >