【问题标题】:Keeping track of which (tweepy) filter caught a tweet跟踪哪个(tweepy)过滤器捕获了一条推文
【发布时间】:2017-07-28 18:18:24
【问题描述】:

我需要跟踪 twitter 上的许多关键字并将推文发送到 MongoDB。我将它用于我的代码:

How can I consume tweets from Twitter's streaming api and store them in mongodb

import json
import pymongo
import tweepy

consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)


class CustomStreamListener(tweepy.StreamListener):
    def __init__(self, api):
        self.api = api
        super(tweepy.StreamListener, self).__init__()

        self.db = pymongo.MongoClient().test

    def on_data(self, tweet):
        self.db.tweets.insert(json.loads(tweet))

    def on_error(self, status_code):
        return True # Don't kill the stream

    def on_timeout(self):
        return True # Don't kill the stream


sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))

to_track = ['keyword1', 'keyword2', 'keyword3']

sapi.filter(track = to_track)

有没有办法让我跟踪每个推文进入的关键字是哪个? (每个都不做grep搜索)

【问题讨论】:

  • 向上向上向上向上向上

标签: python twitter tweepy


【解决方案1】:

我不确定 on_data 函数是如何工作的,但您可以使用 on_status 并执行以下操作:

import tweepy
consumer_key = ''
consumer_secret = ''
access_key = ''
access_secret = ''



auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)


class CustomStreamListener(tweepy.StreamListener):    
    def on_status(self, status):
        tweet = status.text        
        words = tweet.split()
        if 'keyword1' in words:
            print "do something with keyword1"
            self.db.tweets.insert(json.loads(tweet))
        if 'keyword2' in words:
            print "do something with keyword2"
            self.db.tweets.insert(json.loads(tweet))
        if 'keyword3' in words:
            print "do something with keyword3"
            self.db.tweets.insert(json.loads(tweet))
sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))

to_track = ['keyword1', 'keyword2', 'keyword3']

sapi.filter(track = to_track)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2015-11-08
    • 1970-01-01
    • 2017-10-27
    • 2021-12-16
    • 1970-01-01
    相关资源
    最近更新 更多