Tweepy:如何获取一个用户的超过20条推文?

3
根据文档,以下是相关的IT技术内容:
API.user_timeline([id/user_id/screen_name][, since_id][, max_id][, count][, page])

Returns the 20 most recent statuses posted from the authenticating user or the user specified. It’s also possible to request another user’s timeline via the id parameter.

那么我如何从一个人的时间线中获取超过20条推文?文档没有展示如何做到...那个用户需要被认证吗?

2个回答

3
您可以在API.user_timeline([id/user_id/screen_name][, since_id][, max_id][, count][, page])中使用pages参数。对于page值为1,您将从用户时间线获取最新的20条推文,然后在进一步迭代时,当我们增加page = 2的值时,该方法将返回其他20条推文,这些推文比第1页接收到的最旧的推文要旧,您可以将其视为:

假设您的帐户中有120条推文(第1条推文是最旧的,第120条推文是最新的),则:

page = 1将返回(100, 120]

page = 2将返回(80, 100]

...等等

我希望您现在已经理解了页面的概念,现在是实现这些内容的时候了。

no_of_pages = int(raw_input("Please enter the number of tweets: "))
for i in xrange(no_of_pages):
    API.user_timeline("@anmoluppal366", page = i)

你知道如何使用Ruby来完成这个任务吗? - marriedjane875
@marriedjane875 看一下:https://github.com/sferik/twitter/blob/master/examples/AllTweets.md - ZdaR
你为什么这么棒?@anmol_uppal - marriedjane875
该方法中“page”已不再是有效的参数,请在此查看(https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline.html)。并且请记住,您只能获取最近的3200条推文。如果要获取更多,则需要使用其他方法。 - WesternGun

-1

你也可以使用 max_id 参数。像这样:

r0 = api.user_timeline("@donaldtrump") # gives 20 latest tweets
idlast = r0[-1].id # the last id of these 20
r1 = api.user_timeline("@donaldtrump", max_id = idlast) # next 20 tweets older or same age as idlast

最大ID不起作用。 - Irtza Shahan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接