2016-07-06 81 views
0

我想從twitter獲取推文並保存到我嘗試將結果轉換爲JSON的MongoDB數據庫。
這是我的代碼如何將tweepy搜索的結果轉換爲JSON格式

import tweepy 
import json 
import pymongo 
from pymongo import MongoClient 
client=MongoClient() 
db=client.scholarship 

APP_KEY = app key here 
APP_SECRET = app secret here 
OAUTH_TOKEN = access-token here 
OAUTH_TOKEN_SECRET = token secret here 

auth=tweepy.OAuthHandler(APP_KEY,APP_SECRET) 
auth.set_access_token(OAUTH_TOKEN,OAUTH_TOKEN_SECRET) 

api=tweepy.API(auth) 

for tweet in tweepy.Cursor(api.search,q="scholarship",count=100,result_type="recent",include_entities=True,lang="en").items(): 
    tweet=json.dumps(tweet) 
    try: 
     db.daily.insert(tweet) 
    except Exception as e: 
     print "there is a problem ",e 
    else: 
     print "inserted" 

我得到一個錯誤
加薪類型錯誤(再版(O)+「是不是JSON序列化」)

的原因是檢索算法的返回類型是的形式
(貢獻者=無,截斷=假,文本= u'RT @AnwenAb:@OralRobertsU 2016的MedPro的Rx公司\ ufffdEducation就是力量\ ufffd獎學金申請截止日期2016' 年6月1日,is_quote_status = FALSE)

需要建議wha t可以在這裏完成。
謝謝!

回答

2

知道了 因爲你的光標不會返回給你一個JSON。它返回tweepy.models.Status模型的實例。它顯然不能被解析爲JSON。

從模型中得到解析的JSON可以使用

tweet._json 
相關問題