python - 获取 Twitter 用户的整个用户时间线

标签 python twitter tweepy

我想从一个 Twitter 用户那里获取所有用户的推文,到目前为止,这是我想出的:

import twitter
import json
import sys
import tweepy 
from tweepy.auth import OAuthHandler


CONSUMER_KEY = ''
CONSUMER_SECRET= ''
OAUTH_TOKEN=''
OAUTH_TOKEN_SECRET = ''

auth = twitter.OAuth(OAUTH_TOKEN,OAUTH_TOKEN_SECRET,CONSUMER_KEY,CONSUMER_SECRET)


twitter_api =twitter.Twitter(auth=auth)

print twitter_api

statuses = twitter_api.statuses.user_timeline(screen_name='@realDonaldTrump')
print [status['text'] for status in statuses]

请忽略不必要的导入。一个问题是这只会获取用户最近的推文(或前 20 条推文)。是否有可能获得所有用户的推文?据我所知,GEt_user_timeline(?)只允许 3200 条的限制。有没有办法获得至少 3200 条推文?我做错了什么?

最佳答案

您的代码存在一些问题,包括一些多余的导入。特别是,您不需要 import twitterimport tweepy - tweepy 可以处理您需要的一切。您遇到的特定问题是分页,可以在 tweepy 中使用 Cursor 处理。像这样的对象:

import tweepy

# Consumer keys and access tokens, used for OAuth
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

# Creation of the actual interface, using authentication
api = tweepy.API(auth)

for status in tweepy.Cursor(api.user_timeline, screen_name='@realDonaldTrump', tweet_mode="extended").items():
    print(status.full_text)

关于python - 获取 Twitter 用户的整个用户时间线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42225364/

相关文章:

python - buildbot 使用什么算法将构建器分配给奴隶?

python - Stackexchange API 编码

python - 如何使用 ruamel.yaml 生成包含本地标签的 YAML?

php - 正则表达式有条件地用超链接替换 ​​Twitter 主题标签

python - Tweepy Google App Engine 计费错误

python - 从python脚本将unicode插入MySQL数据库

python - tkinter 窗口获取没有窗口顶部的 x、y、几何/坐标

regex - 如何从推文中提取用户名?

html - 社交媒体上的链接跟随按钮不起作用

python - api_limit_status().remaininghits key ERROR ;脚本不工作;无法获取好友