我试图在我的 retweets 搜索中排除 repliesTwython

这是我的代码:

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

如果你想搜索 "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
    

    关于python - Twitter API : How to exclude retweets when searching tweets using Twython,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35938188/

    10-13 04:06