Allow to override `DefaultSpider` by setting
In large projects we use to have a BaseSpider class which is the base class for all spiders. This works well except when using the commands shell and fetch with unknown domains that causes to use the DefaultSpider class as spider instance which may not implement the methods or attributes required by custom extensions or middlewares.
For example, suppose we have the following downloader middleware:
class MyMiddleware(object):
"""This is my fancy downloader middleware."""
def process_response(self, request, response, spider):
# The method `is_error_page` is implemented by default in the
# base spider and overridden per-spider basis.
if spider.is_error_page(response):
raise IgnoreRequest("page with error message")This downloader middleware will cause the commands shell and fetch to fail with domains that do not have a spider in the project, resulting in an exception as follows:
Traceback (most recent call last):
File "/home/rolando/.virtualenvs/myproject/bin/scrapy", line 9, in <module>
load_entry_point('Scrapy==0.25.1', 'console_scripts', 'scrapy')()
File "/home/rolando/.virtualenvs/myproject/local/lib/python2.7/site-packages/scrapy/cmdline.py", line 143, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
File "/home/rolando/.virtualenvs/myproject/local/lib/python2.7/site-packages/scrapy/cmdline.py", line 89, in _run_print_help
func(*a, **kw)
File "/home/rolando/.virtualenvs/myproject/local/lib/python2.7/site-packages/scrapy/cmdline.py", line 150, in _run_command
cmd.run(args, opts)
File "/home/rolando/.virtualenvs/myproject/local/lib/python2.7/site-packages/scrapy/commands/shell.py", line 65, in run
shell.start(url=url)
File "/home/rolando/.virtualenvs/myproject/local/lib/python2.7/site-packages/scrapy/shell.py", line 44, in start
self.fetch(url, spider)
File "/home/rolando/.virtualenvs/myproject/local/lib/python2.7/site-packages/scrapy/shell.py", line 87, in fetch
reactor, self._schedule, request, spider)
File "/home/rolando/.virtualenvs/myproject/local/lib/python2.7/site-packages/twisted/internet/threads.py", line 122, in blockingCallFromThread
result.raiseException()
File "/home/rolando/.virtualenvs/myproject/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 577, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/home/rolando/.virtualenvs/myproject/local/lib/python2.7/site-packages/scrapy/core/downloader/middleware.py", line 46, in process_response
response = method(request=request, response=response, spider=spider)
File "/home/rolando/Projects/myproject/myproject/downloadermw.py", line 19, in process_response
if spider.is_error_page(response):
AttributeError: 'DefaultSpider' object has no attribute 'is_error_page'So, given Scrapy already allows to override some components by a settings, like SPIDER_LOADER_CLASS="myproject.spiderloader.MySpiderLoader", it makes sense to allow to override the base spider class by a setting, for example: DEFAULT_SPIDER_CLASS="myproject.myspider.MySpider".
Source: scrapy/scrapy