如何将tweepy搜索的结果转换为JSON形式 [英] How to convert result of tweepy search to a JSON form

查看:104
本文介绍了如何将tweepy搜索的结果转换为JSON形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从twitter上获取推文,然后保存到我想将结果转换为JSON的MongoDB数据库中. 这是我的代码

I want to fetch tweets from twitter and save to my MongoDB database for which I am trying to convert the result to JSON.
This is my code

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"

我遇到错误
引发TypeError(repr(o)+不可JSON序列化")

I am getting an error
raise TypeError(repr(o) + " is not JSON serializable")

作为搜索结果的返回形式是
(贡献者=无,被截断= False,文本= u'RT @AnwenAb:@OralRobertsU 2016 MedPro Rx Inc \ ufffdEducation is Power \ ufffd Scholarship Application Deadline June 1 2016',is_quote_status = False)

The reason being the return type of serach is of the form
(contributors=None, truncated=False, text=u'RT @AnwenAb: @OralRobertsU 2016 MedPro Rx Inc \ufffdEducation is Power\ufffd Scholarship Application Deadline June 1 2016', is_quote_status=False)

需要在这里可以做什么的建议.
谢谢!

Need suggestions of what can be done here.
Thanks!

推荐答案

知道了 因为您的游标不会返回JSON.它返回tweepy.models.Status模型的实例.而且显然不能将其解析为JSON.

Got it Because your cursor doesn't return you a JSON. It returns instances of tweepy.models.Status model. And it obviously can't be parsed as JSON.

要从模型中获取解析的JSON,可以使用

To get parsed JSON from the model you can use

tweet._json

这篇关于如何将tweepy搜索的结果转换为JSON形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆