0

I am currently using python-twitter module and I want to be able to isolate a string from the return I get with api.GetFollowersPaged(). When I run the code to print the return I get something that looks like this:

[User(ID=...., ScreenName=....)]

how could I isolate just the value of either ID or ScreenName and assign it to another variable. I have tried to use

print ScreenName

but recieve a NameError telling me ScreenName is not defined.

jmfp92
  • 3
  • 3

2 Answers2

0

Never used python-twitter, but checking the docs looks that it returns an array of Users, so you just need to tell the index of the user you wanna get data, or iterate over them:

for user in api.GetFollowersPaged():
    print user.screen_name

Modules Documentation

Source code for twitter.api

RompePC
  • 815
  • 1
  • 8
  • 17
  • When I write this I get aN attribute error on the line with the call to print saying 'int' object has no attribute 'screen_name' – jmfp92 Feb 15 '17 at 07:06
  • Mmm... in the docs says that it returns `next_cursor, previous_cursor, data sequence of twitter. User instances, one for each follower`. Try printing `user[2]` (there should be the users). If not, then you'll have to dig a little more into the docs, as I don't have experience in the API. – RompePC Feb 15 '17 at 07:18
  • Ah now it's a type error where the int has no attribute '__getitem__'. Hey thanks for your time though I appreciate the help – jmfp92 Feb 15 '17 at 07:25
  • Would there be another library you would suggest besides python-twitter – jmfp92 Feb 15 '17 at 07:38
  • If you wanna go brute, start printing in all ways possible what `user` contains and what `api.GetFollowersPaged` contains. If you change to another API, I cannot help you in that, as I didn't never had practise with Twitter APIs in general. But Twitter gives you a [list](https://dev.twitter.com/resources/twitter-libraries) of them, although Twython and tweepy are the most used based in a little [search](https://www.google.es/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=best+python+twitter+api): TwitterAPI also gets some calling. – RompePC Feb 15 '17 at 08:24
0

I did find a way to do what I was trying to do I switched to tweepy and wrote the following script

Users = tweepy.Cursor (api.followers, screen_name= whatever).items ()
while True:
    try:
        User= next (Users)
    except tweepy.TweepError:
        time.sleep (60*15)
        User = next (Users)
    except StopIteration:
        break
   retsn= user.screen_name
   print '@' + retsn

Thanks to everyone for trying to help and hopefully this helps someone someday

jmfp92
  • 3
  • 3