python - Scrapy 抑制处理的错误

标签 python error-handling scrapy twisted

相关代码

def start_requests( self ):
    requests = [ Request( url['url'], meta=url['meta'], callback=self.parse, errback=self.handle_error ) for url in self.start_urls if valid_url( url['url'] )]
    return requests

def handle_error( self, err ):
    # Errors being saved in DB
    # So I don't want them displayed in the logs

我有自己的代码用于在数据库中保存错误代码。我不希望它们显示在日志输出中。我怎样才能抑制这些错误?

请注意,我不想隐藏所有错误 - 只隐藏此处处理的错误。

最佳答案

尝试在 handle_error 中使用 self.skipped.add, self.failed.addisinstance 条件> 方法。

Here is an example

def on_error(self, failure):
    if isinstance(failure.value, HttpError):
        response = failure.value.response
        if response.status in self.bypass_status_codes:
            self.skipped.add(response.url[-3:])
            return self.parse(response)

    # it assumes there is a response attached to failure
    self.failed.add(failure.value.response.url[-3:])
    return failure

关于python - Scrapy 抑制处理的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36682569/

相关文章:

c - 使用 scanf 进行错误检查

python - ScrapyRT 与 Scrapyd

python - Scrapy 文件管道中的 "File (code: 302): Error downloading file"

python - ctrl+B 不适用于 sublimetext2 中的 python

python - 如何在 Web 应用程序的 Docker 镜像中安装并启动 CouchDB 服务器?

r - lapply 的错误处理——输出失败元素的索引

jsp - Adobe CQ : custom error handler only works with .html extension

python - 使用 Pymongo 更新 MongoDB 中的选定字段

python - 写入 csv 时如何保留空值

python - 在 docx 中按顺序处理对象