2

I'm a nub when it comes to python. I literally just started today and have little understanding of programming. I have managed to make the following code work:

from twitter import *

config = {}
execfile("config.py", config)

twitter = Twitter(
    auth = OAuth(config["access_key"], config["access_secret"],       
config["consumer_key"], config["consumer_secret"]))

user = "skiftetse"

results = twitter.statuses.user_timeline(screen_name = user)

for status in results:
print "(%s) %s" % (status["created_at"], status["text"].encode("ascii",   
"ignore"))

The problem is that it's only printing 20 results. The twitter page i'd like to get data from has 22k posts, so something is wrong with the last line of code.

screenshot

I would really appreciate help with this! I'm doing this for a research sentiment analysis, so I need several 100's to analyze. Beyond that it'd be great if retweets and information about how many people re tweeted their posts were included. I need to get better at all this, but right now I just need to meet that deadline at the end of the month.

  • A general rule about discussion of code on the internet: never post screenshots of code/output, always post in text. – orlp Oct 24 '15 at 23:19
  • Please follow [MCVE](http://stackoverflow.com/help/mcve). Cut this down to the minimal code to repro the problem and post in text, not in links. – Prune Oct 25 '15 at 02:14
  • I've pasted the code used, but I could not paste the 20 results part because of a limitation in how many link I could post...since I am a new user. I hope this is good enough to get some answers. Thank you! – uuresearcher Oct 25 '15 at 11:45

1 Answers1

2

You need to understand how the Twitter API works. Specifically, the user_timeline documentation.

By default, a request will only return 20 Tweets. If you want more, you will need to set the count parameter to, say, 50.

e.g.

results = twitter.statuses.user_timeline(screen_name = user, count = 50)

Note, count:

Specifies the number of tweets to try and retrieve, up to a maximum of 200 per distinct request.

In addition, the API will only let you retrieve the most recent 3,200 Tweets.

Terence Eden
  • 14,034
  • 3
  • 48
  • 89
  • Hi! Thanks for the reply. I figured out the count = earlier today as well, but I appreciate you clarifying. If you can only take a maximum of 200 per request, how does one get the total of 3,200? I don't really even need that much. Perhaps 1000 total. Any ideas? Thank you! – uuresearcher Oct 25 '15 at 14:44
  • Take a look at the documentation for your Twitter library. You will have to use cursors to paginate through the list. – Terence Eden Oct 25 '15 at 16:09