0

我想将 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))
4

1 回答 1

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))
于 2012-05-08T11:54:17.973 回答