gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:12:33 25 4
gpt4 key购买 nike

我想从一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com