【问题标题】:how to deal with requests failed in scrapy?如何处理scrapy中失败的请求?
【发布时间】:2019-04-26 03:58:45
【问题描述】:

我想抓取有关网站的信息。我像这样使用代理IP:

def process_request(self, request, spider):
    item = random.choice(ippool)
    request.meta['proxy'] = "http://"+item['ip']
    request.meta['dont_redirect'] = True

但它会引发一些错误:

调试:已爬网 (302)、已爬网 (403)、
信息:忽略响应,500 内部服务器错误

等等。我该如何解决这些问题。

另一方面,由于是代理 IP,因此速度很慢。如果我想在其中添加多进程。我该怎么办?

谢谢。

【问题讨论】:

    标签: python python-3.x scrapy multiprocess


    【解决方案1】:

    要处理特定的 HTTP 状态代码,您可以定义 handle_httpstatus_list

    class MySpider(scrapy.Spider):
        name = "spider_name"
        handle_httpstatus_list = [500, and more here] 
    

    现在不显示了

    INFO: Ignoring response, 500 Internal Server Error
    

    相反,它会将响应交给相关的回调函数。

    【讨论】:

      猜你喜欢
      • 2018-10-22
      • 2022-10-18
      • 2014-08-28
      • 2021-07-13
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      相关资源
      最近更新 更多