【问题标题】:tweepy get tweets between two datestweepy 获取两个日期之间的推文
【发布时间】:2018-09-18 18:03:49
【问题描述】:

我在 Python 中有以下代码:

import tweepy

consumer_key = "..."
consumer_secret = "..."

access_token = "..."
access_token_secret = "..."

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

start_date = datetime.datetime(2018, 1, 19, 12, 00, 00)
end_date = datetime.datetime(2018, 1, 19, 13, 00, 00)

api = tweepy.API(auth)

for tweet in tweepy.Cursor(api.user_timeline, screen_name="@IBM", since=start_date, until=end_date).items():
    print("ID TWEET: " + str(tweet.id))

有没有办法通过用 tweepy 修改光标来获取 start_dateend_date 之间的推文?

我已经尝试过使用since=until= 参数,但它们没有奏效。

提前谢谢你。

【问题讨论】:

  • until 有时间限制:(...) the search index has a 7-day limit. In other words, no tweets will be found for a date older than one week. developer.twitter.com/en/docs/tweets/search/api-reference/…
  • 这可能会有所帮助...https://stackoverflow.com/questions/26205102/making-very-specific-time-requests-to-the-second-on-twitter-api-using-python 或这个https://gist.github.com/alexdeloy/fdb36ad251f70855d5d6

标签: python tweepy


【解决方案1】:

首先,Twitter API 不允许按时间搜索。微不足道,您可以做的是获取推文并随后在 Python 中查看它们的时间戳,但这非常低效。

您可以通过以下代码 sn-p 来做到这一点。

consumerKey = "CONSUMER_KEY"
consumerSecret = "CONSUMER_SECRET"
accessToken = "ACCESS_TOKEN"
accessTokenSecret = "ACCESS_TOKEN_SECRET"

auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)

api = tweepy.API(auth)

username = sys.argv[1]
startDate = datetime.datetime(2011, 6, 1, 0, 0, 0)
endDate =   datetime.datetime(2012, 1, 1, 0, 0, 0)

tweets = []
tmpTweets = api.user_timeline(username)
for tweet in tmpTweets:
    if tweet.created_at < endDate and tweet.created_at > startDate:
        tweets.append(tweet)

while (tmpTweets[-1].created_at > startDate):
    tmpTweets = api.user_timeline(username, max_id = tmpTweets[-1].id)
    for tweet in tmpTweets:
        if tweet.created_at < endDate and tweet.created_at > startDate:
            tweets.append(tweet)

虽然效率极低。它有效,可以帮助我创建自己的机器人。

【讨论】:

  • 谢谢大家的回复!
  • 这对我也有用。请注意,我必须导入示例代码中未显示的datetime
  • 如何让它在 start_date 和 end_date 之间搜索主题标签并获取推文。
  • 我认为你不能。您只能通过search 下载来自给定主题标签的推文,例如:twapi.search(q=query, count=100, since_id=since_id, max_id=str(last_id - 1), tweet_mode='extended') 过去 10 天。唯一的解决方法是您知道 tweet_id,无论 Twitter API 设置的时间限制如何,您都可以下载它。
【解决方案2】:

我刚刚使用 until (可选运算符),它似乎工作得很好。我是这样使用的:

tweets = tw.Cursor(api.search,
                   q=search_words,
                   lang="en",
                   since=date_since,
                   until=date_until,
                   result_type="recent"
                   ).items(2)

【讨论】:

  • since 在 3.8 版中已作为 api.search 的搜索参数被删除。但 until 仍然存在:返回在给定日期之前创建的推文......日期应格式化为 YYYY - MM-DD
  • 包括刚刚开始日期只是给我当前系统日期的数据。当我尝试包含结束日期时,我收到错误,“局部变量 'csvFile' 在分配之前引用”
猜你喜欢
  • 2021-03-10
  • 2020-12-26
  • 2015-08-02
  • 2018-12-21
  • 2018-01-11
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
相关资源
最近更新 更多