运行脚本后,在成功生成.csv文件中的数据时出现此错误。这是错误
Traceback (most recent call last):
File "f:\softwares\anaconda\lib\site-packages\twisted\internet\defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "C:\Users\Danial Taimoor\Desktop\facebook\facebook\pipelines.py", line 21, in process_item
self.collection.insert(dict(item))
AttributeError: 'MongoDBPipeline' object has no attribute 'collection'
2018-08-02 10:06:04 [scrapy.extensions.logstats] INFO: Crawled 1 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
这是settings.py代码
ITEM_PIPELINES = {
'facebook.pipelines.MongoDBPipeline': 300,
}
MONGODB_SERVER='localhost'
MONGODB_PORT = 27017
MONGODB_DB= 'posts'
MONGODB_COLLECTION='fb_posts'
pipeline.py
from pymongo import MongoClient
from scrapy.conf import settings
class MongoDBPipeline(object):
def __init_(self):
connection=MongoClient(
settings['MONGODB_SERVER'],
settings['MONGODB_PORT']
)
db=connection[settings['MONGODB_DB']]
self.collection=db[settings['MONGODB_COLLECTION']]
def process_item(self,item,spider):
self.collection.insert(dict(item))
return item
这是我第一次尝试mongodb!