简体   繁体   English

PyMongo 集合对象不可调用

[英]PyMongo Collection Object Not Callable

I'm trying to create a Reddit scraper that takes the first 100 pages from the Reddit home page and stores them into MongoDB.我正在尝试创建一个 Reddit 抓取工具,它从 Reddit 主页获取前 100 页并将它们存储到 MongoDB 中。 I keep getting the error:我不断收到错误:

TypeError: 'Collection' object is not callable. If you meant to call the 'insert_one' method on a 'Collection' object it is failing because no such method exists.

Here is my code这是我的代码

import pymongo
import praw
import time


def main():
    fpid = os.fork()
    if fpid!=0:
        # Running as daemon now. PID is fpid
        sys.exit(0)

    user_agent = ("Python Scraper by djames v0.1")
    r = praw.Reddit(user_agent = user_agent)    #Reddit API requires user agent

    conn=pymongo.MongoClient()
    db = conn.reddit
    threads = db.threads


    while 1==1:    #Runs in an infinite loop, loop repeats every 30 seconds
        frontpage_pull = r.get_front_page(limit=100)    #get first 100 posts from reddit.com

        for posts in frontpage_pull:    #repeats for each of the 100 posts pulled
            data = {}
            data['title'] = posts.title
            data['text'] = posts.selftext
            threads.insert_one(data)
        time.sleep(30)

if __name__ == "__main__":
    main()

insert_one() was not added to pymongo until version 3.0. insert_one()直到 3.0 版才被添加到 pymongo。 If you try calling it on a version before that, you will get the error you are seeing.如果您尝试在之前的版本上调用它,您将看到您看到的错误。

To check you version of pymongo, open up a python interpreter and enter:要检查您的 pymongo 版本,请打开一个 python 解释器并输入:

import pymongo
pymongo.version

The legacy way of inserting documents with pymongo is just with Collection.insert() .使用 pymongo 插入文档的传统方式只是使用Collection.insert() So in your case you can change your insert line to:因此,在您的情况下,您可以将插入行更改为:

threads.insert(data)

For more info, see pymongo 2.8 documentation有关更多信息,请参阅pymongo 2.8 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 集合 object 不是 PyMongo 的可调用错误 - Collection object is not callable error with PyMongo “进程池”中的“集合”对象在pymongo中不可调用错误 - 'Collection' object is not callable error in pymongo with process Pool pymongo 数据库的 insert() 和 insert_one() 方法都不能调用“Collection”对象 - 'Collection' object is not callable for both insert() and insert_one() method of a pymongo Database Flask MongoKit'Collection'对象不可调用 - Flask MongoKit 'Collection' object is not callable PyMongo奇怪的错误“ TypeError:'Database'对象不可调用。” - PyMongo Strange Error “TypeError: 'Database' object is not callable.” pymongo 集合对象序列化 __getnewargs__ 方法 - pymongo collection object serializing __getnewargs__ method 如何检查字符串是否在 pymongo.collection.collection object - How to check if string is in pymongo.collection.collection object UpdateMany 报错:'Collection' object is not callable - UpdateMany gives a error: 'Collection' object is not callable 插入pymongo集合时,“ str”对象不支持项目分配 - 'str' object does not support item assignment when inserting to pymongo collection Pymongo:collection.find_one()返回字典对象而不是文档 - Pymongo: collection.find_one() returns dictionary object instead of document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM