Search code examples
pythonpython-2.7twittertweepyapi-design

Tweepy - Multiple User Search


Trying to perform what I thought was a very basic exercise. I have a list of 7000+ customers and I want to import some of their attributes from twitter. I am trying to figure out how to input several user names into my tweepy query, but I am not getting any luck with below.

#!/usr/bin/python
import tweepy
import csv #Import csv
import os

# Consumer keys and access tokens, used for OAuth
consumer_key = 'MINE'
consumer_secret = 'MINE'
access_token = 'MINE'
access_token_secret = 'MINE'

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)


api = tweepy.API(auth)
# Open/Create a file to append data
csvFile = open('ID.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)

users = ('IBM' or 'massmutual')
user = api.get_user(screen_name = users)

csvWriter.writerow([user.screen_name, user.id, user.followers_count, user.description.encode('utf-8')])

print user.id
csvFile.close()

Solution

  • You will have to do it in a loop, you will make list of users and get user instance for all one by one in loop.

    #!/usr/bin/python
    import tweepy
    import csv #Import csv
    import os
    
    # Consumer keys and access tokens, used for OAuth
    consumer_key = 'MINE'
    consumer_secret = 'MINE'
    access_token = 'MINE'
    access_token_secret = 'MINE'
    
    # OAuth process, using the keys and tokens
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    
    
    api = tweepy.API(auth)
    # Open/Create a file to append data
    csvFile = open('ID.csv', 'a')
    #Use csv Writer
    csvWriter = csv.writer(csvFile)
    
    users = ['IBM','massmutual','user3',.......]
    for user_name in users:
        user = api.get_user(screen_name = user_name)
        csvWriter.writerow([user.screen_name, user.id, user.followers_count, user.description.encode('utf-8')])
        print user.id
    
    
    csvFile.close()