在推特推特流式传输时忽略转发

2024-04-25 21:25:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试运行一个简单的脚本,将流式直播推特。几次尝试过滤掉转发都没有成功。我的流中仍然有手动转发(文本为“RT@”)。 我尝试过其他方法,包括link和{a2}。在

在我学习的过程中,我的代码与以下代码非常相似:link

我该怎么做才能忽略转发?在

下面是我的代码片段:

class StreamListener(tweepy.StreamListener):

    def on_status(self, status):
        if (status.retweeted) and ('RT @' not in status.text):
            return

    description = status.user.description
    loc = status.user.location
    text = status.text
    coords = status.coordinates
    geo = status.geo
    name = status.user.screen_name
    user_created = status.user.created_at
    followers = status.user.followers_count
    id_str = status.id_str
    created = status.created_at
    retweets = status.retweet_count
    bg_color = status.user.profile_background_color

    # Initialize TextBlob class on text of each tweet
    # To get sentiment score from each class
    blob = TextBlob(text)
    sent = blob.sentiment

Tags: 代码textnameonstatuslinkdescriptionat
1条回答
网友
1楼 · 发布于 2024-04-25 21:25:29

您可以创建另一个函数来调用您的StreamListener中的{}内部。以下是对我有用的东西:

def analyze_status(text):
    if 'RT' in text[0:3]:
        print("This status was retweeted!")
        print(text)
    else:
        print("This status was not retweeted!")
        print(text)

class MyStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        analyze_status(status.text)
    def on_error(self, status_code):
        print(status_code)

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth=twitter_api.auth, listener=myStreamListener)
myStream.filter(track=['Trump'])

结果如下:

^{pr2}$

这不是最优雅的解决方案,但我相信它能解决您所面临的问题。在

相关问题 更多 >