流式传播Twitter推文时忽略转发

时间:2017-04-26 20:50:17

标签: python python-3.x twitter tweepy

我正在尝试运行一个简单的脚本来传输实时推文。过滤掉转发的几次尝试都没有成功。我仍然在我的流中获得手动转发(带有“RT @”文本)。 我尝试了其他方法,包括linklink

在我学习的过程中,我的代码非常类似于以下内容: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

1 个答案:

答案 0 :(得分:1)

您可以做的是创建另一个函数来调用on_status中的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'])

产生以下结果:

This status was not retweeted!
@baseballcrank @seanmdav But they won't, cause Trump's name is on it. I can already hear their stupidity, "I hate D… 
This status was retweeted!
RT @OvenThelllegals: I'm about to end the Trump administration with a single tweet
This status was retweeted!
RT @kylegriffin1: FLASHBACK: April 2016

SAVANNAH GUTHRIE: "Do you believe in raising taxes on the wealthy?"

TRUMP: "I do. I do. Inc… 

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

相关问题