I m new in python. help me out. THE PROBLEM: i got all tweets of respective user. I want n [suppose(n=20)] no of latest tweet of respective user.how to fetch n no of tweets ?? below is my code:
def get_all_tweets(screen_name):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
alltweets = []
# make initial request for most recent tweets (200 is the maximum allowed count)
new_tweets = api.user_timeline(screen_name=screen_name,count=200)
alltweets.extend(new_tweets)
print len(alltweets)
oldest = alltweets[-1].id - 1
while len(new_tweets) > 0:
print "getting tweets before %s" % (oldest)
new_tweets = api.user_timeline(screen_name=screen_name, 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))
f = open('testfile.txt', 'w+')
for tweet in alltweets:
f.write(str(tweet.text))
f.write('\n')
f.close()
tweets = (tweet.text)
return tweets
pass
# to encode the data
reload(sys)
sys.setdefaultencoding('utf-8')
if __name__ == '__main__':
get_all_tweets("xyz")