gpt4 book ai didi

python - PyMongo + Scrapy =名称必须是basestring的实例

转载 作者:行者123 更新时间:2023-11-30 23:33:57 24 4
gpt4 key购买 nike

我正在尝试使用 scrapy + MongoDB (PyMongo) 抓取 Spider,但收到错误:名称必须是基本字符串的实例。

由于我的 Spider 正在工作,因为它正在将数据抓取到 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

最佳答案

似乎您打算连接到本地主机端口 27017,但您却使用这些值作为键来从设置中获取值。您是指这个吗?

 def __init__(self):
self.server = 'localhost'
self.port = '27017'
self.db = 'IngressoRapido'
self.col = 'Shows'

关于python - PyMongo + Scrapy =名称必须是basestring的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18516700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com