用户的整个用户时间线

用户的整个用户时间线

本文介绍了获取 Twitter 用户的整个用户时间线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get the all of a user tweets from one Twitter user and so far this is what I came up with:

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 条推文?我做错了什么?

Please ignore the unnecessary imports. One problem is that this only gets a user's recent tweets (or the first 20 tweets). Is it possible to get all of a users tweet? To my knowledge, the GEt_user_timeline (?) only allows a limit of 3200. Is there a way to get at least 3200 tweets? What am I doing wrong?

推荐答案

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

There's a few issues with your code, including some superfluous imports. Particularly, you don't need to import twitter and import tweepy - tweepy can handle everything you need. The particular issue you are running into is one of pagination, which can be handled in tweepy using a Cursor object like so:

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)

这篇关于获取 Twitter 用户的整个用户时间线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 22:03