如何使用 Tweepy 从 Twitter 中搜索历史数据

How to search for historical data from Twitter with Tweepy

我是 Python 的新手,需要一些帮助。我正在尝试在研究项目中使用特定关键字搜索特定 Twitter 用户从 2014 年到 2018 年的历史推文,并对我的代码提出一些问题。

  1. 我找不到有关如何将输出作为大量推文而不是一长串推文的代码。这可能吗?
  2. 有谁知道如何将端点从 Twitter Developer 连接到代码?我将使用高级 API 帐户。
  3. 由于以下功能,我在代码中遇到错误:from 和 filter,有没有办法解决这个问题?我试图将它插入与查询相同的“”中,但这没有用。
#Endpoint from Twitter Developer 
endpoint = 'https://api.twitter.com/1.1/tweets/search/30day/dev.json'

#All keywords
query = 'xxxx' or 'yyyy'

#Search code
search = tweepy.Cursor(api.search, q = query, "from:TwitterUser -filter:retweets", lang = 'en').items()
for item in search:
    print (item.text)

另外一个与我的代码无关的问题是,是否可以搜索特定用户在某些推文上的转发和点赞?比方说,我正在搜索 user1 的推文,然后我可以看到 user2、user3 和 user4 转发和喜欢了 user1 的推文的次数吗?

感谢您的帮助!

干杯

我认为 tweepy 目前不支持高级搜索端点(此外,我看到您的代码达到了 30 天的端点,对于您的用例,您需要访问完整的存档搜索端点).

对于高级搜索,我建议您查看 TwitterAPI, or the search-tweets-python library that we (Twitter) provide. In the case of the latter, it also supports the counts endpoint, but be aware that these are only available in the paid tier, not the free sandbox. Also, note that the search syntax on the premium search API is different to standard search. Check the documentation

关于你最后一个与代码无关的问题 - 不,没有 API 可以让你找到该信息。