我已经修改了Using Middleware 中的代码以忽略 Scrapy 中的重复项。
from scrapy.exceptions import DropItem
from scrapy import log
import os.path
class IgnoreDuplicates():
def __init__(self):
self._cu_file = open("crawled_urls.txt", "a+")
self._crawled_urls = set([line.strip() for line in self._cu_file.readlines()])
def process_request(self, request, spider):
if request.url in self._crawled_urls:
raise DropItem("Duplicate product scrape caught by IgnoreDuplicates at <%s>" % (url))
else:
self._crawled_urls.add(request.url)
self._cu_file.write(request.url + '\n')
log.msg("IgnoreDuplicates recorded this url " + request.url, level=log.DEBUG)
return None
我还在 settings.py 中添加了中间件模块:
SPIDER_MANAGER_CLASS = 'slybot.spidermanager.SlybotSpiderManager'
EXTENSIONS = {'slybot.closespider.SlybotCloseSpider': 1}
ITEM_PIPELINES = {'slybot.dupefilter.DupeFilterPipeline': 1}
SPIDER_MIDDLEWARES = {'slybot.middleware.IgnoreDuplicates': 500, 'slybot.spiderlets.SpiderletsMiddleware': 999} # as close as possible to spider output
PLUGINS = ['slybot.plugins.scrapely_annotations.Annotations']
SLYDUPEFILTER_ENABLED = True
PROJECT_DIR = 'slybot-project'
FEED_EXPORTERS = {
'csv': 'slybot.exporter.SlybotCSVItemExporter',
}
CSV_EXPORT_FIELDS = None
try:
from local_slybot_settings import *
except ImportError:
pass
不会调用 process_request 函数。我尝试更改 settings.py 中中间件键的值,以便在 SpiderletsMiddleware 之前和之后执行它。但是异常和日志消息没有显示在输出中。
如何确保调用中间件?