简体   繁体   English

使用tweepy获取用户的旧推文

[英]Get old tweets by user using tweepy

I am trying to gather the tweets of a user navalny , from 01.11.2017 to 31.01.2018 using tweepy . 我正在尝试使用tweepynavalny日到navalny日收集用户navalny的推文。 I have ids of the first and last tweets that I need, so I tried the following code: 我具有所需的第一条和最后一条推文的ids ,因此我尝试了以下代码:

import tweepy

consumer_key = '' 
consumer_secret = ''
access_token = ''
access_token_secret = ''

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

t = api.user_timeline(screen_name='navalny', since_id = 933000445307518976, max_id = 936533580481814529)

However, the returned value is an empty list. 但是,返回的值是一个空列表。

  • What is the problem here? 这里有什么问题?

  • Are there any restrictions on the history of tweets that I can get? 我可以获得的推文历史记录是否有限制?

  • What are possible solutions? 有什么可能的解决方案?

Quick answer: Using Tweepy you can only retrieve the last 3200 tweets from the Twitter REST API for a given user. 快速解答:使用Tweepy,您只能从Twitter REST API中检索给定用户的最后3200条推文。 Unfortunately the tweets you are trying to access are older than this. 不幸的是,您尝试访问的推文比这更旧。

Detailed answer: I did a check using the code below: 详细答案:我使用以下代码进行了检查:

import tweepy
from tweepy import OAuthHandler


def tweet_check(user):
    """
    Scrapes a users most recent tweets
    """
    # API keys and initial configuration
    consumer_key = ""
    consumer_secret = ""
    access_token = ""
    access_secret = ""
    # Configure authentication
    authorisation = OAuthHandler(consumer_key, consumer_secret)
    authorisation.set_access_token(access_token, access_secret)
    api = tweepy.API(authorisation)
    # Requests most recent tweets from a users timeline
    tweets = api.user_timeline(screen_name=user, count=2, 
                               max_id=936533580481814529)
    for tweet in tweets:
        tid = tweet.id
        print(tid)


twitter_users = ["@navalny"]

for twitter_user in twitter_users:
    tweet_check(twitter_user)

This test returns nothing before 936533580481814529 该测试在936533580481814529之前未返回任何内容

Using a seperate script I scraped all 3200 tweets, the max Twitter will let you scrape and the youngest tweet id I can find is 943856915536326662 使用一个单独的脚本,我抓取了所有3200条推文,最大的Twitter信息可让您抓取,我可以找到的最小的推文ID为943856915536​​326662

Seems like you have run into Twitter's tweet scraping limit for user timelines here. 好像您在这里遇到了Twitter对用户时间线的推文抓取限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM