【问题标题】:Adding scrapy middleware from current script从当前脚本添加scrapy中间件
【发布时间】:2016-03-04 01:48:43
【问题描述】:

我已经在一个 python 脚本中设置了我的爬虫:

import sys
import csv
import scrapy
from scrapy.http import FormRequest
from scrapy.crawler import CrawlerProcess


class MyItem(scrapy.Item):
    test = scrapy.Field()


class Spider(scrapy.Spider):
    start_urls = [
        "blah.com",
    ]

    def parse(self, response):
        blahblah = MyItem()
        # Some Code
        yield blahblah


class crawler:
    def start(self):
        process = CrawlerProcess({
            'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
            'LOG_LEVEL': 'INFO',
            'FEED_FORMAT': 'csv',
            'FEED_URI': 'Output.csv'
        })
        process.crawl(Spider)
        process.start()

app = crawler()
app.start()

这是完美的工作。 现在如何添加一个scrapy中间件,例如 process_spider_exception(response, exception, spider) 在此脚本中并通过将其添加到 CrawlerProcess 设置来使用它?

【问题讨论】:

    标签: python scrapy web-crawler


    【解决方案1】:

    我已经使用扭曲的errback 解决了这个问题,它可以在它的callback 后面使用,并在callback 抛出exception 时处理错误。

    相关[问题]:how to scrapy handle dns lookup failed

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 2014-03-02
      相关资源
      最近更新 更多