Twitter 搜索 API 使用正则表达式过滤推文:"no tweets found"

Twitter Search API using regex to filter tweets: "no tweets found"

我目前正在尝试制作一个推特机器人,它应该回复一条推文,它使用正则表达式过滤并回复它。 相关代码如下所示:

questionRegex = re.compile(regex here)
def searchWeatherRequest(weatherReport) :
    for tweet in tweepy.Cursor(api.search,
                                q=questionRegex,
                                lang="en",
                                since=today).items(1):
        try:
            tweetId = tweet.user.id
            username = tweet.user.screen_name
            print ('\Tweet by: @' + username)
            tweet.retweet()
            api.update_status("@" + username + "Today's weather" + weatherReport)
            print (tweet.text)
        except tweepy.TweepError as e:
            print (e.reason)
        except StopIteration:
            break

    time.sleep(3600)

但是每当我 运行 代码时,我都会收到消息 "no tweets found"(即使在发布了一条与正则表达式匹配的推文之后,所以我知道这不仅仅是因为没有推文那将匹配它)。 我还尝试分步过滤推文(首先,我只使用一个词过滤推文,然后使用正则表达式过滤这些推文)但这也不起作用。 有谁知道我做错了什么。我阅读了多篇关于此的文章和问题,但 none 的解决方案似乎有效。 我读到一个问题,您无法使用正则表达式过滤推文,但其他答案却另有建议。你真的不能使用正则表达式,还是我遇到了一个简单的编码错误?

遗憾的是,正则表达式在这里不起作用。这是因为 q= 期待一个字符串通过,因此不会解释您传递的正则表达式,相反我相信它要么只是错误,要么将 re.compile(regex here) 作为字符串本身,当然,不太可能出现很多(如果有的话)结果。

看来你目前的方法行不通。解决方法是使用 Twitter's standard operators. You could build up strings using filter operations that when passed to the Cursor, essentially act the same way as your regex did. Keep in mind though that there are character limits and overly complicated queries may also be rejected. You can find details on that in the search tweets docs.

另一种选择是进行相当笼统的搜索,然后使用您的正则表达式从那里过滤结果。与您的问题非常相似的回答者分享了一些文章 here

希望对您有所帮助,让您走上正确的道路。