How to track more than a single term in Tweepy?

不想你离开。 提交于 2019-12-08 13:35:31

问题


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


回答1:


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

I tend to do this in something like the following:

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'

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.



来源:https://stackoverflow.com/questions/13299206/how-to-track-more-than-a-single-term-in-tweepy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!