根据主题标签和user_mentions过滤推文

时间:2015-02-18 14:52:38

标签: twitter tweepy twitter-streaming-api

我希望使用流式api获取实时推文,然后根据我拥有的主题标签和user_mentions列表对其进行过滤。只应提取那些在主题标签列表中的一个项目中具有任何主题标签或在user_mentions列表中的一个项目中具有user_mention的推文。

我想首先获取所有推文,然后在阅读这些推文时,我会提取hashtags和user_mentions字段,看看该集合与我所拥有的集合之间是否存在交集。问题是如何获取所有推文。
应该在stream.filter()中指定为track参数。
或者有更简洁的方法吗?

1 个答案:

答案 0 :(得分:1)

如果拥有主题标签和用户提及,您可以直接跟踪这些标签。

# Lets say I have a user_mentions list...
user_mentions = ['@this_guy', '@that_guy', '@those_guys']

# And a hashtags list...
hashtags = ['#ThisHastag', '#ThatHashtag']

# You can merge them into one list of terms to be tracked
track_terms = user_mentions + hashtags

# The list "track_terms" now looks like this:
# track_terms = ['@this_guy', '@that_guy', '@those_guys', '#ThisHastag', '#ThatHashtag']

# Passing these to .filter() will collect tweets containing those terms
stream.filter(track=track_terms)