我正在尝试构建一个应用程序来使用流式 Twitter API 跟踪特定用户的一些术语。
我基于此 tutorial 使用 tweepy 为流式 api 创建了一个有效的 python 脚本.但是,它只有在我按条款或用户 ID 跟踪推文时才有效,但现在两者都可以。当我尝试使用它们进行搜索时,api 会返回任何用户的推文。我的代码在这里:
#Acessando a API do twitter com as chaves
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token_key, access_token_secret)
#Chamando o Listener com o tweepy
api = tweepy.API(auth)
#Chama o stream e passa o que buscar no twitter.
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
list_users = ['11111','22222'] #Some ids
list_terms = ['term1','term2'] #Some terms
sapi.filter(follow=list_users, track=list_terms)
这两个变量(list_users
, list_terms
)分别是用户id列表和术语列表。
如何按用户和条款过滤推文流?有什么办法可以用 tweepy 过滤器做到这一点吗?还是我应该在检索推文后进行验证?
最佳答案
Twitter 流式 API 使用 OR
逻辑评估不同的条件,即返回带有术语和来自用户的推文的并集。因此,您必须实现自定义 on_data
函数才能使用 AND
进行过滤。
请注意,您最多只能条件 5000 users and 400 terms ,并且由于速率限制可能是一个问题,因此您需要为 api 提供产生较低推文流的条件,并在后处理中使用所有其余条件过滤传入数据。
You can track up to 5,000 users and 400 keywords -- the rate limiting indeed takes effect at 1% of the Firehose, so if at any moment the tweet volume from the union of your keywords and users rises above 1% of all tweets happening in "real time" on the Firehose, you'll get up to 1% of the tweets along with a rate limit notice informing you of how many tweets you missed.
关于python - Tweepy 跟踪条款和关注用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14083968/