Twitter API:使用Twython搜索推文时如何排除转发 [英] Twitter API: How to exclude retweets when searching tweets using Twython

查看:321
本文介绍了Twitter API:使用Twython搜索推文时如何排除转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to exclude retweets and replies in my Twython search.

这是我的代码:

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函数不能真正起作用.

The problem is that the -RT function isn't really working.

我尝试了@forge建议,尽管它确实打印了tweets不是转推或回复,但当我将它们合并到下面的代码中时,该机器人仍会找到推文,转推,引号和回复.

I've tried @forge suggestion, and while it does print the if tweets are not retweets or replies, when I incorporate them into the code below, the bot still finds tweets, retweets, quotes and replies.

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?

Any ideas? Is there a filter:quotes?

推荐答案

正确的语法是-filter:retweets.

如果您要搜索"search phrase""another search phrase"字词并排除转发,则query应为:

If you would like to search on terms "search phrase" or "another search phrase" and exclude retweets, then the query should be:

query = "search_phrase OR another_search_phrase -filter:retweets"

要排除回复,也可以这样添加-filter:replies:

To exclude replies as well, add -filter:replies like this:

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

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

This should be working, you can verify it by checking the status fields in_reply_to_status_id and retweeted_status:

    如果in_reply_to_status_id为空,
  • 状态不是答复
  • 如果状态中没有字段retweeted_status
  • ,则不是转发状态
  • Status is not a reply if in_reply_to_status_id is empty
  • Status is not a retweet if it doesn't have the field 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

这篇关于Twitter API:使用Twython搜索推文时如何排除转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆