如何在 Tweepy 中跟踪多个术语? [英] How to track more than a single term in Tweepy?

查看:31
本文介绍了如何在 Tweepy 中跟踪多个术语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Tweepy 并想跟踪两个单独的术语,wordA"和wordB"(意味着每条推文将包含其中之一),但我也想将它们的结果存储在单独的结构中.是否可以在同一个 auth 对象上有两个单独的流侦听器?任何演示如何执行此操作的代码示例将不胜感激.谢谢

I am using Tweepy and would like to track two separate terms, 'wordA' and 'wordB' (meaning each tweet will contain either) but I also want to store their results in separate structures. Is it possible to have two separate stream listeners on the same auth object? any code examples demonstrating how to do this would be appreciated. Thanks

推荐答案

每个用户只允许一个流,因此您必须在收到数据后将它们拆分.

You're only allowed a single stream per user, so you'll have to split them out after the data has been received.

我倾向于这样做:

import tweepy
from tweepy.utils import import_simplejson
json = import_simplejson()
tracklist1=[wordA, wordAA]
tracklist2=[wordB, wordBB]

class CustomStreamListener(tweepy.StreamListener):

    def on_data(self, data):
        if 'in_reply_to_status_id' in data:
            temp=json.loads(data)
            words = [word.lower().strip('!,.:?"') for word in temp['text'].split()]
            if set(words) & set(tracklist1):
                print 'match A'
            elif set(words) & set(tracklist):
                print 'match B'
            else:
                print 'no match found'

对我来说效果很好,并且使用 tracklist1 和 tracklist2 的列表可以让您为您关注的每个主题构建更复杂的搜索.你总会得到一些不匹配的,因为 twitter 匹配用户名以及流 API 上的文本.

Works well enough for me, and the use of lists for tracklist1 and tracklist2 allow you to build a more complex search for each topic you're after. You'll always get some that don't match as twitter matches against user names as well as text on the streaming API.

要正确执行此操作,您可能需要过滤掉所有不是字母数字的内容,而不是像我在上面的示例中所做的那样仅去除最常见的标点符号.

To do this properly you'll probably want to filter out everything that isn't alphanumerics instead of just stripping the most common punctuation as I've done in the example above.

这篇关于如何在 Tweepy 中跟踪多个术语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆