运行 Scrapy 项目时,出现以下报错:
Traceback (most recent call last):
File "G:\Anaconda\Anaconda\lib\site-packages\twisted\internet\defer.py", line 857, in _runCallbacks
current.result = callback( # type: ignore[misc]
File "G:\Anaconda\Anaconda\lib\site-packages\scrapy\utils\defer.py", line 162, in f
return deferred_from_coro(coro_f(*coro_args, **coro_kwargs))
File "G:\Python\images360\images360\pipelines.py", line 33, in process_item
self.db[name].insert(dict(item))
File "G:\Anaconda\Anaconda\lib\site-packages\pymongo\collection.py", line 2779, in __call__
raise TypeError(
TypeError: 'Collection' object is not callable. If you meant to call the 'insert' method on a 'Collection' object it is failing because no such method exists.
报错为:
TypeError: 'Collection' object is not callable. If you meant to call the 'insert' method on a 'Collection' object it is failing because no such method exists.
翻译:
类型错误:“Collection”对象不可调用。如果您打算对“Collection”对象调用“insert”方法,则会失败,因为不存在此类方法。
错误原因:
PyMongo 4.0.* 中 Collection.insert 方法被移除,使用 Collection.insert_one 或者 Collection.insert_many 替代:
collection.insert_one({'my': 'document'})
collection.insert_many([{'doc': 2}, {'doc': 3}])
相关更改链接:PyMongo 4 collection-insert-is-removed
官方文档:PyMongo 4 Migration Guide — PyMongo 4.0.2 documentation 可查看 PyMongo 相关信息
CMD 查看 PyMongo 版本:
pip list -v