我试图用scrapy + MongoDB(PyMongo)来抓取蜘蛛,我收到错误:名称必须是basestring的一个实例。PyMongo + Scrapy = name必须是basestring的一个实例
由于我的蜘蛛工作,因为它被刮的数据到JSON,我猜的错误是在我的新管道,这里是源代码:
import pymongo
from scrapy import log
from scrapy.conf import settings
from scrapy.exceptions import DropItem
class MongoDBPipeline(object):
def __init__(self):
self.server = settings['localhost']
self.port = settings['27017']
self.db = settings['IngressoRapido']
self.col = settings['Shows']
connection = pymongo.Connection(self.server, self.port)
db = connection[self.db]
self.collection = db[self.col]
def process_item(self, item, spider):
err_msg = ''
for banda, local in item.items():
if not local :
err_msg += 'Faltando local %s da banda %s\n' % (banda, item['banda'])
if err_msg:
raise DropItem(err_msg)
self.collection.insert(dict(item))
log.msg('Item written to MongoDB database %s/%s' % (self.db, self.col),
level=log.DEBUG, spider=spider)
return item
新手上的错误,谢谢! –
顺便说一句,现在我的收藏没有被创建,有什么建议,为什么? –
只是疯狂的猜测:当你运行'mongo'时,你连接到'test'数据库。在你的情况下,你需要发出'使用IngressoRapido',然后才能在'show collections'的输出中看到你的'Shows'集合。 – cababunga