gpt4 book ai didi

python - 跟踪哪个(tweepy)过滤器捕获了一条推文

转载 作者:太空宇宙 更新时间:2023-11-04 05:12:30 24 4
gpt4 key购买 nike

我需要在 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 搜索)

最佳答案

我不确定 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)

关于python - 跟踪哪个(tweepy)过滤器捕获了一条推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42666960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com