【问题标题】:get tweets with specific status ids using tweepy使用 tweepy 获取具有特定状态 ID 的推文
【发布时间】:2018-03-27 22:41:05
【问题描述】:

我有一个我需要获取的推文的特定状态 ID 的列表。 tweepy 文档提供以下内容:

 API.get_status(id)

Returns a single status specified by the ID parameter.
Parameters: id – The numerical ID of the status.
Return type:    Status object

我无法弄清楚如何使用它或找到任何示例。 这甚至是正确的吗?

我的 id 列表有 2240 项长,看起来像这样:

response_ids = [717289507981107201, 717289501337509888, ..., 716684885411237888]

这些 id 是从我已经拥有的推文的“in_response_to_status_id”字段中获得的(我想将我拥有的推文与他们为响应而编写的推文相匹配)。

我基本上想写类似的东西

for id in response_ids:
    tweet = API.get_status(id)

任何关于如何做到这一点的帮助,或关于这是否可行的建议,非常感谢。

【问题讨论】:

    标签: python twitter status tweepy


    【解决方案1】:

    最好使用“statuses_lookup”命令。更多信息在下面的链接http://docs.tweepy.org/en/v3.5.0/api.html#API.statuses_lookup

    在运行以下程序之前,获取消费者密钥和令牌。

    import tweepy
    consumer_key = xxxx
    consumer_secret = xxxx
    access_token = xxxx
    access_token_secret = xxxx
    
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    
    api = tweepy.API(auth)
    
    tweets = api.statuses_lookup(id_list) # id_list is the list of tweet ids
    tweet_txt = []
    for i in tweets:
        tweet_txt.append(i.text)
    

    【讨论】:

      【解决方案2】:

      我想我已经解决了。

      get_status 似乎是正确的使用方法,尽管我最初遇到了一些分页错误问题。我已经破解了一些响应另一个similar problem 的代码来提出这个解决方案:

      def paginate(iterable, page_size):
          while True:
              i1, i2 = itertools.tee(iterable)
              iterable, page = (itertools.islice(i1, page_size, None),
                      list(itertools.islice(i2, page_size)))
              if len(page) == 0:
                  break
              yield page
      
      index = 0
      for page in paginate(response_ids, 1):
          result = api.get_status(response_ids[index])._json
          index += 1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-05
        • 2020-09-07
        • 1970-01-01
        • 2015-04-07
        • 2016-10-20
        • 2016-05-18
        • 2015-10-08
        • 2020-03-12
        相关资源
        最近更新 更多