账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    python 中导入mongodb 问题 报错
    26
    0
    raceback (most recent call last):
      File "/home/shenjianlin/.local/lib/python3.4/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks
        current.result = callback(current.result, *args, **kw)
      File "/home/shenjianlin/my_project/Espider/Espider/pipelines/MongoPipelines.py", line 25, in process_item
        res = self.Mongodb.find(key_word)
      File "/usr/lib64/python3.4/site-packages/pymongo/collection.py", line 3100, in __call__
        self.__name)
    TypeError: 'Collection' object is not callable. If you meant to call the 'find' method on a 'Database' object it is failing because no such method exists.
    2018-10-11 19:30:41 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://api.zsxq.com/v1.10/files/51114252822224/download_url>; (referer: https://wx.zsxq.com/dweb/)
    2018-10-11 19:30:41 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://api.zsxq.com/v1.10/files/51114252822254/download_url>; (referer: https://wx.zsxq.com/dweb/)
    2018-10-11 19:30:41 [scrapy.core.scraper] ERROR: Error processing {'file_url': 'https://files.zsxq.com/lpawwLc8YufA3n22KiwDGuc1-7Mu?attname=%E5%8D%8E%E7%99%BB%E5%9B%BD%E9%99%85-MEMS%EF%BC%8C%E6%9C%BA%E5%99%A8%E4%BA%BA%E5%92%8C%E6%99%BA%E8%83%BD%E6%B1%BD%E8%BD%A6-2018.03-32%E9%A1%B5.pdf&e=1874736000&token=kIxbL07-8jAj8w1n4s9zv64FuZZNEATmlU_Vm6zD:IfFwug2_hLLVlHfp8RUz-ky4x3A=',
     'name': '华登国际-MEMS,机器人和智能汽车-2018.03-32页.pdf'}
    Traceback (most recent call last)
    
    
    import pymongo
    from scrapy.exceptions import DropItem
    
    
    class MongodbPipeline(object):
    
        def __init__(self, MongodbHost, MongodbPort, MongodbName, MongodbCollection):
            self.MongodbHost = MongodbHost
            self.MongodbPort = MongodbPort
            self.MongodbName = MongodbName
            self.MongodbCollection = MongodbCollection
    
        @classmethod
        def from_crawler(cls, crawler):
            return cls(MongodbHost=crawler.settings.get('MONGODB_HOST'), MongodbPort=crawler.settings.get('MONGODB_PORT'),
                       MongodbName=crawler.settings.get('MONGODB_DBNAME'),
                       MongodbCollection=crawler.settings.get('MONGODB_COLLECTION'))
    
        def open_spider(self, spider):
            self.Client = pymongo.MongoClient(self.MongodbHost, self.MongodbPort)
            self.Mongodb = self.Client[self.MongodbName]
    
        def process_item(self, item, spider):
            key_word = {"file_url": item['file_url'], "name": item['name']}
            res = self.Mongodb.find(key_word)
            for i in res:
                if i:
                    raise DropItem("Duplicate item found: %s" % item)
                else:
    
                    self.Mongodb.insert({"file_url": item['file_url'], "name": item['name']})
    
                    return item
    
        def close_spider(self, spider):
            self.Client.close()
    
    
    1
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 其实喜欢你 普通会员 1楼

      在Python中导入MongoDB时,可能出现以下错误:

      错误代码:AttributeError: 'module' object has no attribute 'MongoDB'

      原因:这个错误通常表示你在尝试访问一个模块的属性,但该模块并没有这个属性。例如,如果你正在尝试导入MongoDB,但是MongoDB模块没有MongoDB属性,你将得到这个错误。

      解决方法: 1. 检查模块的导入路径:确保你导入的模块的路径正确,即在你的Python脚本中,你应该导入模块的完整路径,而不是部分路径。 2. 检查模块名称:确保你正在尝试导入的模块名称是正确的。如果你导入了错误的模块,Python将无法找到它。 3. 检查模块文件名:确保你正在尝试导入的模块文件名是正确的。如果你导入了错误的文件名,Python将无法找到它。 4. 检查模块版本:确保你导入的模块版本与你的Python版本兼容。不同的Python版本可能会支持不同的模块。

      例如,如果你正在尝试导入MongoDB,但MongoDB模块没有MongoDB属性,你应该使用以下代码:

      python from pymongo import MongoClient

      而不是:

      python import mongodb

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部