【问题标题】:Tweepy check if a tweet is a retweetTweepy 检查推文是否为转推
【发布时间】:2018-10-07 16:54:51
【问题描述】:

我开始在 Python 中使用 Tweepy 3.6.0,但我有一些问题。

首先,我想获取推文列表(使用 api.search 方法),而不是转推。我发现了一些奇怪的东西。尝试使用他的 ID 和 author_name 访问推文。它会自动重定向到原始推文(不同的 ID 和作者姓名)。

经过一番搜索,我发现人们在谈论“retweeted_status”键。如果钥匙退出,那么它就是一个RT。但在我下面的示例中,我的推文对象中没有 retweeted_status,但重定向到原始推文就在这里。

我理解错了吗?

谢谢

【问题讨论】:

  • 你的例子在哪里?
  • 我的意思是我的解释,误会见谅
  • 没关系,不用担心。检查这个问题的答案,看看它是否有助于澄清你的理解stackoverflow.com/questions/26181130/…
  • 这似乎有效,结果包含原始推文:tweets= api.user_timeline(id=user['id'], count=30,include_rts=True); for tweet in tweets: print tweet
  • 我看到了这个帖子,但它是关于时间线推文的。我目前正在使用 api.search() 方法:/我更新了线程,因为我忘了说这个

标签: python twitter tweepy


【解决方案1】:

您可以选择仅搜索转发或从搜索查询中排除所有转发。

搜索不转发“-filter:retweets”

for tweet in tweepy.Cursor(api.search, q='github -filter:retweets',tweet_mode='extended').items(5):

只搜索转推“filter:retweets”

 for tweet in tweepy.Cursor(api.search, q='github filter:retweets',tweet_mode='extended').items(5):

额外信息:

虽然您可以在搜索查询中直接排除转发,但也很容易找到一条推文是否为转发,因为所有转发都以“rt @UsernameOfAuthor”开头。您可以通过执行基本的 if 语句来查看推文是否是转推,以查看推文是否以 rt 开头。

首先进行基本查询并将信息保存到变量中。

for tweet in tweepy.Cursor(api.search, q='github',tweet_mode='extended').items(5):
    # Defining Tweets Creators Name
    tweettext = str( tweet.full_text.lower().encode('ascii',errors='ignore')) #encoding to get rid of characters that may not be able to be displayed
    # Defining Tweets Id
    tweetid = tweet.id

然后打印信息用于演示目的

    # printing the text of the tweet
    print('tweet text: '+str(tweettext))
    # printing the id of the tweet
    print('tweet id: '+str(tweetid))

然后有if语句判断是否转发

# checking if the tweet is a retweet (this method is basic but it will work)
if tweettext.startswith("rt @") == True:
    print('This tweet is a retweet')
else:
    print('This tweet is not retweet')

【讨论】:

  • 非常感谢您的回答,我会尝试一下 :) 但是“-filter:retweets”似乎有点问题。我试了很多次都没有找到最近的推文:/
  • Twitter 的免费 API 非常不可靠,它们真的不提供存档视图。我添加了 if 语句来检查它是否也转发了我的回复,如果另一种方式效果不佳,也可以尝试一下。 :)
  • 那是真的啊哈。我找到了另一种方法:“isRT = hasattr(tweet, 'retweeted_status')”
  • 有多种方法可以判断一条推文是否为转发,我给出了我更喜欢使用的方法。您可以检查的另一种方法甚至只是检查推文是否包含名为“retweeted_status”的属性。给猫剥皮的方法有很多:)
猜你喜欢
  • 2013-09-23
  • 2018-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
  • 2012-07-23
相关资源
最近更新 更多