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

时间:2020-05-17 23:02:02

标签: python twitter 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)

但是,在打印结果时,它将以任何顺序返回包含该短语的所有推文。中间是否有单词无关紧要。 如何更改此设置,以便找到仅包含此短语的推文?

1 个答案:

答案 0 :(得分:3)

根据我的研究,无法通过Twitter API的正则表达式来指定完全匹配。

但是,您可以使用正则表达式对返回的推文进行后处理:

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…
相关问题