我试图在我的搜索中排除retweets
和replies
。
这是我的代码:
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 suggestion,虽然它确实打印了if tweets不是转发或回复,但当我将它们合并到下面的代码中时,bot仍然会找到tweets、转发、引用和回复。
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
应为:要同时排除答复,请按如下方式添加
-filter:replies
:这应该可以工作,您可以通过检查状态字段
in_reply_to_status_id
和retweeted_status
来验证它:in_reply_to_status_id
为空,则状态不是答复retweeted_status
,则Status不是retweet使用
Twython
:相关问题 更多 >
编程相关推荐