简体   繁体   中英

Twitter API - how to get real time tweets by user

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?

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:

  • Tweets created by the user.
  • Tweets which are retweeted by the user.
  • Replies to any Tweet created by the user.
  • Retweets of any Tweet created by the user.
  • Manual replies, created without pressing a reply button (eg “@twitterapi I agree”).

It doesn't return:

  • Tweets mentioning the user (eg “Hello @twitterapi!”).
  • Manual Retweets created without pressing a Retweet button (eg “RT @twitterapi The API is great”).
  • Tweets by protected users.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM