【问题标题】:Ignoring Retweets When Streaming Twitter Tweets流式传输 Twitter 推文时忽略转推
【发布时间】:2017-09-24 10:51:06
【问题描述】:

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

据我了解,我的代码与以下代码非常相似:link

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

这是我的代码的 sn-p:

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

【问题讨论】:

  • status 对象是什么样的?
  • 您的逻辑似乎有点混乱 - (status.retweeted) and ('RT @' not in status.text) 只会返回“官方”转推。也许您应该使用(status.retweeted) or ('RT @' in status.text) 来排除“官方”和“手动”转推

标签: python python-3.x twitter tweepy


【解决方案1】:

您可以做的是创建另一个函数以在您的StreamListener 中的on_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… 

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

【讨论】:

  • 这不是假设“官方”转推(我认为不一定以“RT @”开头)没有被转推吗?
  • 我让StreamListener() 运行了很长时间,其中一个热门主题仅在status.retweeted 时打印,并且它从不打印。也许 .retweeted 属性没有按预期工作?
  • 嗯,这似乎不寻常。你可以试试hasattr(status, 'retweeted_status'),它似乎相当一致地用于传统的“转推”和“引用”,假设它是通过 twitter 界面完成的。
猜你喜欢
  • 1970-01-01
  • 2018-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-19
相关资源
最近更新 更多