有没有办法通过Twitter手柄或Twitter ID使用Tweepy
来提取实时推文?
我已尝试使用Twitter Streaming API
,但我只能通过关键字过滤推文:
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'])
无论如何我可以通过推特用户名下拉推文吗?
答案 0 :(得分:6)
您应该可以像这样传递follow
关键字:
stream.filter(follow=['user_id'])
它会收到一个用户ID列表。
从API
文档中,应返回:
它不会返回:
注意强>
您可以同时指定track
和follow
值,这会产生包含输入用户创建的输入关键字或推文的推文。