【问题标题】:How to fetch tweets of a given user?如何获取给定用户的推文?
【发布时间】:2015-12-18 13:09:28
【问题描述】:

我正在尝试使用 Twitter API 来自动获取给定用户的最后一条推文,但我遇到了麻烦:/

我正在使用这个库:https://code.google.com/p/python-twitter/

我安装了它,一切似乎都正常,但是当我尝试获取用户的时间线时,我只得到了我所有的时间线:(

这是我的代码:

import twitter

api = twitter.Api(consumer_key='***', consumer_secret='****', access_token_key='***', access_token_secret='****')
statuses = api.GetUserTimeline('@twitterapi')
print [s.text for s in statuses]

我错过了什么吗?

【问题讨论】:

  • 您是否尝试从GetUserTimeline 的第一个参数中删除'@'(即api.GetUserTimeline('twitterapi'))?
  • 是的,结果相同:/当我尝试使用 id 而不是用户名时,我收到一个错误,提示用户不存在:/
  • 如果你只使用api = twitter.Api()会发生什么?我认为您不需要公开推文的 api 密钥。
  • 不,我收到了这个错误:twitter.TwitterError: [{u'message': u'Bad Authentication data', u'code': 215}]

标签: python twitter


【解决方案1】:

我认为您必须提供 userId 而不是 screen_name 才能使 GetUserTimeLine 工作。

此外,尽管您可能期望这将返回与该用户的主页状态页面等效的内容,但事实并非如此。相反,它只返回来自该用户的推文。

twitter API 文档提到了另一种方法 - GetFriendsTimeline,但是,尽管在文档中列出,但据我所知,它似乎并不存在。

【讨论】:

    【解决方案2】:

    您必须明确输入screen_name=user_id=,否则该值默认为经过身份验证的用户。

    示例:

    statuses = api.GetUserTimeline(screen_name='some_handle')
    

    statuses = api.GetUserTimeline(user_id=22233344)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      相关资源
      最近更新 更多