jueves, 9 de marzo de 2017

Where My Twitter Followers Come From?



From time to time I like to see where my followers come from. So I take a look at  twitter analytics in demographic data section I got this result.


Javier Méndez Lirón twitter1 Asesores económicos independientes 


Last week-end, bored to death, and seeing that twitter only shows the ten first countries of the list I decided to use Tweepy to get the whole list.

As I have developer credentials, and tweepy package installed the first thing was accessing Data. This task can be done easily using the next code list.




import tweepy


consumer_key = 'YOUR-CONSUMER-KEY'
consumer_secret = 'YOUR-CONSUMER-SECRET'

try:
    auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
    api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
    c = tweepy.Cursor(api.followers_ids, id = 'EconomAsesores')
    ids = []
    for page in c.pages():
        ids.append(page)
    print ("ids=", ids)
   
except tweepy.TweepError:
    print ("Error")





In this way we can obtain the ids of all followers (happily “@EconomAsesores” only had 1,012 at that moment).

Once we have the list of ids we can iterate them to get followers user name and location.

for item in [IDS LIST]:
    user = api.get_user(item)
    print (user.screen_name,';',user.location)


As many users don’t identify their location we used NA (not available) for missing values. On the other hand, when a user provide various places we took only the first one.
With these precisions we can work with data but the results are slightly different to those  provided by twitter as you can see in the next charts. Perhaps, because of we have 236 NAs in my dataset.



Javier Méndez Lirón twitter2 Asesores económicos independientes

Javier Méndez Lirón twitter3 Asesores económicos independientes

Javier Méndez Lirón twitter4 Asesores económicos independientes


If you know a better way to do this don’t hesitate let me know.

No hay comentarios:

Publicar un comentario