【问题标题】:Twitter API: How to exclude retweets when searching tweets using TwythonTwitter API:如何在使用 Twython 搜索推文时排除转推
【发布时间】:2016-06-26 13:52:47
【问题描述】:

我正在尝试在我的 Twython 搜索中排除 retweetsreplies

这是我的代码:

from twython import Twython, TwythonError

app_key = "xxxx"
app_secret = "xxxx"
oauth_token = "xxxx"
oauth_token_secret = "xxxx"   

naughty_words = [" -RT"]
good_words = ["search phrase", "another search phrase"]
filter = " OR ".join(good_words)
blacklist = " -".join(naughty_words)
keywords = filter + blacklist

twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret) 
search_results = twitter.search(q=keywords, count=100)

问题在于-RT 函数并没有真正起作用。

编辑:

我尝试了@forge 建议,虽然它确实打印了如果推文不是转发或回复,但当我将它们合并到下面的代码中时,机器人仍然会找到推文、转发、引用和回复。

twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret) query = 'beer OR wine AND -filter:retweets AND -filter:replies' 
response = twitter.search(q=query, count=100) 
statuses = response['statuses'] 
try: 
for tweet in statuses: 
try: 
twitter.retweet(id = tweet["id_str"]) 
except TwythonError as e: 
print e 
except TwythonError as e: 
print e

有什么想法吗?有filter:quotes吗?

【问题讨论】:

  • 我尝试在我的坏词字段中包含“-filter:retweets”和“-filter:replies”,但没有得到任何运气。让 api 只返回推文而不回复引用或转发真的有这么难吗?
  • 你也可以作为[Craig Addyman]给的教程使用-craigaddyman.com/mining-all-tweets-with-python

标签: python twitter twython


【解决方案1】:

正确的语法是-filter:retweets

如果您想搜索术语"search phrase""another search phrase" 并排除转发,那么query 应该是:

query = "search_phrase OR another_search_phrase -filter:retweets"

要排除回复,请像这样添加-filter:replies

query = "search_phrase OR another_search_phrase -filter:retweets AND -filter:replies"

这应该可以工作,您可以通过检查状态字段in_reply_to_status_idretweeted_status 来验证它:

  • 如果in_reply_to_status_id 为空,则状态不是回复
  • 如果没有字段retweeted_status,则状态不是转推

Twython:

import twython

twitter = twython.Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) 

query = 'wine OR beer -filter:retweets AND -filter:replies' 
response = twitter.search(q=query, count=100)
statuses = response['statuses']
for status in statuses:
    print status['in_reply_to_status_id'], status.has_key('retweeted_status')

# Output should be (None, False) to any status

【讨论】:

猜你喜欢
  • 2011-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-06
  • 2013-08-19
  • 2014-06-28
  • 2012-09-26
  • 2015-12-10
相关资源
最近更新 更多