Reputation: 1246
Is there a way to pull real time tweets using Tweepy
by Twitter handle or Twitter ID?
I've tried using the Twitter Streaming API
but I'm only able to get tweets filtered by keywords:
import tweepy
import json
# Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
# Variables that contains the user credentials to access Twitter API
access_token = "INSERT ACCESS TOKEN"
access_token_secret = "INSERT ACCESS TOKEN SECRET"
consumer_key = "INSERT CONSUMER KEY"
consumer_secret = "INSERT CONSUMER SECRET"
# This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
if __name__ == '__main__':
# This handles Twitter authentication and connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
stream.filter(track=['european central bank'])
Is there anyway I can pull down tweets by Twitter username?
Upvotes: 4
Views: 6798
Reputation: 6834
You should be able to pass the follow
keyword like this:
stream.filter(follow=['user_id'])
It receives a list of user IDs.
From the API
docs, this should return:
It doesn't return:
Note
You can specify both track
and follow
values and it will result in tweets with the input keyword or tweets created by the input user.
Upvotes: 6