保存Tweepy对象

时间:2012-05-08 10:58:12

标签: python django

我想将tweepy对象(用户状态)保存到我的数据库中,在输入以下代码后,它只保存具有特定用户名的用户而不是保存所有用户对象。如何保存列表中的所有用户对象?

查看:

consumer_key=""
consumer_secret=""
access_token=""
access_token_secret=""
auth=tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api=tweepy.API(auth)
db=MySQLdb.connect("localhost","","","")
cur=db.cursor()

def tweetstream(request):
    statuses=Cursor(api.list_timeline, owner=request.user, slug='Testy').items(20)
    for status in statuses:

        print "%s\t%s\t%s\t%s" % (status.text,
                                  status.author.screen_name,
                                  status.created_at,
                                  status.source,)

        cur.execute("INSERT INTO tweets VALUES (%s, %s, %s, %s)", (status.text,
                                                                   status.author.screen_name,
                                                                   status.created_at,
                                                                   status.source))

        return render_to_response('dashboard.html',{'statuses': statuses},context_instance=RequestContext(request))

1 个答案:

答案 0 :(得分:0)

您必须将return语句从for块中删除!这就是为什么要完成一次插入......

for status in statuses:

    print "%s\t%s\t%s\t%s" % (status.text,
                              status.author.screen_name,
                              status.created_at,
                              status.source,)

    cur.execute("INSERT INTO tweets VALUES (%s, %s, %s, %s)", (status.text,
                                                               status.author.screen_name,
                                                               status.created_at,
                                                               status.source))

return render_to_response('dashboard.html',{'statuses': statuses},context_instance=RequestContext(request))