【发布时间】:2020-12-12 00:12:23
【问题描述】:
我正在使用 tweepy 从 twitter 的流媒体 API 中提取推文。然后我用它来自动回复那个用户。
例如,如果我想从中提取实时推文,然后回复唐纳德特朗普,我会使用:
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
import json
class StdOutListener(StreamListener):
def on_data(self, data):
clean_data = json.loads(data)
tweetId = clean_data["id"]
tweet = "YOUR MESSAGE HERE"
respondToTweet(tweet, tweetId)
def setUpAuth():
auth = tweepy.OAuthHandler("consumer_token", "consumer_secret")
auth.set_access_token("access_token", "Access_token_secret")
api = tweepy.API(auth)
return api, auth
def followStream():
api, auth = setUpAuth()
listener = StdOutListener()
stream = Stream(auth, listener)
stream.filter(follow=["25073877"], is_async=True)
def respondToTweet(tweet, tweetId):
api, auth = setUpAuth()
api.update_status(tweet, in_reply_to_status_id=tweetId, auto_populate_reply_metadata=True)
if __name__ == "__main__":
followStream()
如果你运行我上面的代码,你会注意到它确实回复了唐纳德·特朗普,但也回复了他推文的所有新回复
我需要添加什么才能从流中排除对他的推文的回复?
提前感谢您的帮助。
【问题讨论】:
-
是的 - 我想解决这个问题的原因是它会生成大量被视为垃圾邮件的回复。
标签: python api twitter streaming tweepy