2017-04-26 689 views
1

我正在尝试运行一个简单的脚本,它将传送实时推文。多次尝试过滤出转发失败。我仍然在我的流中获得手动转发(文本“RT @”)。 我试过其他方法,包括linklink忽略转推Twitter推文时的转推

由于我学习,我的代码是非常类似于以下内容: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 
+0

'status'对象是什么样的? – ninesalt

+1

你的逻辑似乎有点困惑 - '(status.retweeted)和('RT @'不在status.text中)'只会返回“官方”转推。也许你应该使用'(status.retweeted)或('RT @'status.text)'排除“官方”和“手动”转发 – asongtoruin

回答

0

你可以做的是创造另一个函数在StreamListeneron_status内调用。这是一种适合我的工作:

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… 

这还不是最完美的解决方案,但我相信它解决你所面对的问题。

+0

这不会假设“官方”转推(我相信不必须以“RT @”开始)不被转推? – asongtoruin

+0

我让'StreamListener()'长时间运行一个流行的话题,只有在'status.retweeted'打印时才会打印。 .retweeted属性可能无法按预期工作? – Brian

+0

嗯,这似乎不寻常。您可以尝试'hasattr(status,'retweeted_status')',这似乎被传统的“转推”和“引用”相当一致地使用,假设它是通过twitter界面完成的。 – asongtoruin