【问题标题】:Twython to extract tweetsTwython 提取推文
【发布时间】:2019-04-24 00:48:01
【问题描述】:

我正在使用 Twython twitter API 来提取推文。但我只收到 100 条推文。我想提取从 2013 年 12 月 10 日到 2014 年 3 月 10 日的推文。我在搜索功能中提到了 count=1000。

速率限制是 100,我明白了。有没有办法在给定的时间段内获取这些推文而没有任何速率限制。

 from twython import Twython
 import csv
 from dateutil import parser
 from dateutil.parser import parse as parse_date
 import datetime
 from datetime import datetime
 import pytz

 utc=pytz.UTC

 APP_KEY = 'xxxxxxxxxxx'    
 APP_SECRET = 'xxxxxxxxxxx'
 OAUTH_TOKEN = 'xxxxxxxx'  # Access Token here
 OAUTH_TOKEN_SECRET = 'xxxxxxxxxxx'  

 t = Twython(app_key=APP_KEY, app_secret=APP_SECRET, oauth_token=OAUTH_TOKEN,      oauth_token_secret=OAUTH_TOKEN_SECRET)

 search=t.search(q='AAPL', count="1000",since='2013-12-10')
 tweets= search['statuses']


 for tweet in tweets:
     do something

【问题讨论】:

    标签: python twitter twython


    【解决方案1】:

    通过Search API 访问推文时存在限制。看看这个Documentation

    Search API 通常只提供过去一周的推文。

    当您尝试检索过去 3/4 个月的推文时,您不会收到旧推文。

    【讨论】:

    • 还有其他出路吗?
    【解决方案2】:

    使用 Twython,搜索 API 是有限的,但我仅使用 get_user_timeline 就取得了成功。

    我解决了一个类似的问题,我想从用户那里获取最后 X 条推文。

    如果您阅读文档,对我有用的技巧是跟踪我阅读的最后一条推文的 id,并在我的下一个请求中使用 max_id 阅读该推文。

    对于您的情况,您只需要修改 while 循环以在“created_at”的某些条件下停止。像这样的东西可以工作:

    # Grab the first 200 tweets
    last_id = 0
    full_timeline = 200
    result = t.get_user_timeline(screen_name='NAME', count = full_timeline)
    
    for tweet in result:
        print(tweet['text'], tweet['created_at'])
        last_id = tweet['id']
    
    # Update full timeline to see how many tweets were actually received
    # Full timeline will be less than 200 if we read all the users tweets
    full_timeline = len(result)
    
    # 199 cause result[1:] is used to trim duplicated results cause of max_id
    while full_timeline >= 199:
        result = t.get_user_timeline(screen_name='NAME', count = 200, max_id = last_id)
    
        # Since max_id is inclusive with its bound, it will repeat the same tweet we last read, so trim out that tweet
        result = result[1:]
        for tweet in result:
            print(tweet['text'], tweet['created_at'])
            last_id = tweet['id']
    
        # Update full_timeline to keep loop going if there are leftover teweets
        full_timeline = len(result)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多