如何从twitter获取旧数据

时间:2017-05-16 06:50:32

标签: python-2.7 twitter tweepy

我正在使用tweepy api来获取旧推文

这是我的代码,只能从Twitter发送最新的3200条推文。

代码:

import tweepy #https://github.com/tweepy/tweepy
import pymssql

#Twitter API credentials
access_key = ''
access_secret = ''
consumer_key = ''
consumer_secret = ''

#connect to local database
conn = pymssql.connect('(local)','','','mydbname')
x = conn.cursor()

def get_all_tweets(sc):

    #authorize twitter, initialize tweepy
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth)

    #initialize a list to hold all the tweepy Tweets
    alltweets = []  

    #make initial request for most recent tweets (200 is the maximum allowed count)
    new_tweets = api.user_timeline(screen_name = sc,count=200)

    #save most recent tweets
    alltweets.extend(new_tweets)

    #save the id of the oldest tweet less one
    oldest = alltweets[-1].id - 1

    #keep grabbing tweets until there are no tweets left to grab
    while len(new_tweets) > 0:
        print "getting tweets before %s" % (oldest)

        #all subsiquent requests use the max_id param to prevent duplicates
        new_tweets = api.user_timeline(screen_name = sc,count=200,max_id=oldest)

        #save most recent tweets
        alltweets.extend(new_tweets)

        #update the id of the oldest tweet less one
        oldest = alltweets[-1].id - 1

        print "...%s tweets downloaded so far" % (len(alltweets))

    print len(alltweets)

    #save tweets to database
    for tweet in alltweets:
            insert_post_details(tweet)

#insert tweets data into database
def insert_post_details(tweet):
    try:
        qry="INSERT INTO MPTTwitterPosts(Id,Created_At,Favorite_Count ,Retweet_Count,Text) VALUES(%s,%s,%s,%s,%s)"
        x.execute(qry, (str(tweet.id), tweet.created_at,tweet.favorite_count,tweet.retweet_count,str(tweet.text.encode("utf-8","ignore"))))
        conn.commit()
    except Exception,e:
        print str(e)

if __name__ == '__main__':
    #pass in the username of the account you want to download
    get_all_tweets("Google")

如果我尝试使用max_id获取较旧的推文,则会显示空白的推文列表

所以我怎么能得到旧的推文,任何建议都会非常有用。

谢谢

0 个答案:

没有答案