如何缩小 Tweepy 中的搜索结果?

How to narrow down search results in Tweepy?

目前,我正在搜索这样的推文:

public_tweets = api.search("what do we do?", count=10, result_type="recent")
for tweet in public_tweets:
    tweet_text = tweet.text
    print(tweet_text)

但是,在打印结果时,它 returns 所有包含该短语的推文以任何顺序排列。中间是否有单词或其他任何内容都没有关系。 我如何更改它以便找到仅包含此短语的推文?

根据我的研究,没有办法指定精确匹配,比如通过正则表达式到 Twitter API。

但是,您可以post-使用正则表达式处理您返回的推文:

import re, tweepy

def twitter():
    auth = tweepy.OAuthHandler("", "")
    auth.set_access_token("","")

    api = tweepy.API(auth, wait_on_rate_limit=True,
                     wait_on_rate_limit_notify=True)

    public_tweets = api.search("what do we do?", count=100, result_type="recent")
    for tweet in public_tweets:
        tweet_text = tweet.text
        if re.search(r"[Ww]hat do we do\?",tweet_text):
            print(tweet_text)

if __name__ == '__main__':
    twitter()

刚才的结果:

RT @traceyfutures: This is disappointing. She really was our best hope. What do we do now?
RT @oochqmemes: tsukki: (is unconscious)
yama: tsukki's not breathing! what do we do?!
kuroo: i'll give him mouth to mouth
tsukki: (wakes u…