我正在使用 Scrapy 抓取多个网站,这些网站可能共享冗余信息。
对于我抓取的每个页面,我将页面的 url、它的标题和它的 html 代码存储到 mongoDB 中。我想避免数据库中的重复,因此,我实现了一个管道以检查是否已经存储了类似的项目。在这种情况下,我提出了一个DropItem
例外。
我的问题是,每当我通过异常删除一个项目时DropItem
,Scrapy 会将项目的全部内容显示到日志(标准输出或文件)中。当我提取每个抓取页面的整个 HTML 代码时,如果出现丢失,整个 HTML 代码将显示在日志中。
我怎样才能在不显示其内容的情况下默默地放下一个项目?
感谢您的时间!
class DatabaseStorage(object):
""" Pipeline in charge of database storage.
The 'whole' item (with HTML and text) will be stored in mongoDB.
"""
def __init__(self):
self.mongo = MongoConnector().collection
def process_item(self, item, spider):
""" Method in charge of item valdation and processing. """
if item['html'] and item['title'] and item['url']:
# insert item in mongo if not already present
if self.mongo.find_one({'title': item['title']}):
raise DropItem('Item already in db')
else:
self.mongo.insert(dict(item))
log.msg("Item %s scraped" % item['title'],
level=log.INFO, spider=spider)
else:
raise DropItem('Missing information on item %s' % (
'scraped from ' + item.get('url')
or item.get('title')))
return item