关于python:Twitter API-如何获得用户的实时推文

Twitter API - how to get real time tweets by user

是否可以通过Twitter句柄或Twitter ID使用Tweepy提取实时推文?

我尝试使用Twitter Streaming API,但是我只能获取按关键字过滤的推文:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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'])

我是否可以通过Twitter用户名下拉推文?


您应该能够像这样传递follow关键字:

1
stream.filter(follow=['user_id'])

它接收用户ID列表。

API文档中,应返回:

  • 用户创建的推文。
  • 由用户转发的推文。
  • 回复用户创建的任何推文。
  • 用户创建的任何推文的转发。
  • 手动回复,无需按回复按钮即可创建(例如,@ twitterapi我同意)。

它不返回:

  • 提及用户的推文(例如a?Hello @twitterapi!a€?)。
  • 无需按"转发"按钮即可创建手动转发(例如RT @twitterapi API非常棒)。
  • 受保护用户的推文。

注意
您可以同时指定trackfollow值,这将导致使用输入关键字的推文或由输入用户创建的推文。