Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter User Profile can be extracted by this

Tags:

python

tweepy

I am able to extract the mentioned details about a twitter user using Tweepy API. I want to do it for a list of users. Can anyone help me to this?

import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
CONSUMER_KEY = 'ABC'
CONSUMER_SECRET = 'ABC'
ACCESS_KEY = 'ABC'
ACCESS_SECRET = 'ABC'
class TweetListener(StreamListener):
    # A listener handles tweets are the received from the stream.
    #This is a basic listener that just prints received tweets to standard output

    def on_data(self, data):
        print data
        return True

    def on_error(self, status):
        print status

auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
api = tweepy.API(auth)

auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
twitterStream = Stream(auth,TweetListener())
user = api.get_user('User Name')
print user.screen_name
print user.description
print user.followers_count
print user.statuses_count
print user.url

This code is ready to use anyone can use it with his/her own credentials for a single user profile.

like image 764
Sourabh Choudhary Avatar asked Dec 25 '22 08:12

Sourabh Choudhary


2 Answers

Finally exercising and reading a lot I get the answer to my question.you can try this

import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
CONSUMER_KEY = 'ABC'
CONSUMER_SECRET = 'ABC'
ACCESS_KEY = 'ABC'
ACCESS_SECRET = 'ABC'
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
api = tweepy.API(auth)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
class TweetListener(StreamListener):
    # A listener handles tweets are the received from the stream.
    #This is a basic listener that just prints received tweets to standard output

    def on_data(self, data):
        print data
        return True

    def on_error(self, status):
        print status
#search
api = tweepy.API(auth)
twitterStream = Stream(auth,TweetListener())
test = api.lookup_users(user_ids=['17006157','59145948','157009365'])
for user in test:
    print user.screen_name
    print user.name
    print user.description
    print user.followers_count
    print user.statuses_count
    print user.url

This code is ready to use just put your valid keys in place of ABC & get the users profile.you need to get the IDs first.

like image 147
Sourabh Choudhary Avatar answered Dec 27 '22 22:12

Sourabh Choudhary


Your code simply interacts with your twitter account; to find information on a specific user or group of users you should look them up using the api.lookup_users(user_ids=[]) query.

You'd do it like this:

#boring auth you already have
import tweepy
from tweepy import OAuthHandler
CONSUMER_KEY = 'ABC'
CONSUMER_SECRET = 'ABC'
ACCESS_KEY = 'ABC'
ACCESS_SECRET = 'ABC'
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
api = tweepy.API(auth)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)

#search
api = tweepy.API(auth)
test = api.lookup_users(user_ids=['1123728482,5539932'])

This gives you a list of two tweepy.models.User objects:

[<tweepy.models.User object at 0x103995090>, <tweepy.models.User object at 0x1039950d0>]

You can replace the list in user_ids with a list of up to 100 ids, twitter won't let you search any more than that at once, though. Once you have your list of User objects, you can access different properties (for a list, check out the tweepy doc for the User class, line 113).

like image 29
Luigi Avatar answered Dec 27 '22 20:12

Luigi