2016-02-26 41 views
1

我正在嘗試創建一個reddit刮取器,它從reddit主頁獲取前100頁,並將它們存儲到MongoDB中。這是我在stackoverflow上的第一篇文章,所以如果我的文章格式不正確,我很抱歉。我不斷收到錯誤:PyMongo集合對象不可Callable

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. 

這裏是我的代碼

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() 
+0

順便說一句,你可以說'而1:'來代替'而1 == 1:' 。 – zondo

回答

2

insert_one()沒有添加到pymongo直到3.0版本。如果您之前嘗試在某個版本上調用它,則會看到您所看到的錯誤。

要檢查你pymongo的版本,打開了Python解釋器,然後輸入:

import pymongo 
pymongo.version 

與pymongo插入文檔的傳統方式是隻是Collection.insert()。所以你的情況,你可以改變你的插入線:

threads.insert(data) 

欲瞭解更多信息,請參閱pymongo 2.8 documentation

+0

就是這樣。謝謝! – djames