gpt4 book ai didi

python - 推特 API : How to exclude retweets when searching tweets using Twython

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:21 24 4
gpt4 key购买 nike

我试图在我的 Twython 搜索中排除 转推回复

这是我的代码:

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

如果您想搜索术语“搜索短语”“另一个搜索短语”并排除转推,那么查询应该是:

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,则 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 - 推特 API : How to exclude retweets when searching tweets using Twython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35938188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com