You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Tweepy API在Spyder仅能获取一周推文,如何获取一年内历史推文?

How to Retrieve Tweets Older Than a Week with Tweepy

Hey there! I’ve dealt with this exact limitation before, so let me break down what’s going on and how you can get that year’s worth of historical tweets you need.

First off: The search() method you’re using calls Twitter’s Standard Search API, which has a hard limit of only returning tweets from the last 7 days. That’s not a flaw in your code or Tweepy—it’s an official restriction from Twitter for the free standard API tier.

To access tweets from the past year, you’ll need to use Twitter’s Premium Search API (or the Enterprise Search API if you need massive-scale data). Here’s what you need to do:

1. Get the Right API Access

  • Head to the Twitter Developer Platform and upgrade your project to include Premium Search access. There are two tiers:
    • 30-Day Premium: Accesses tweets from the last 30 days (not enough for your year-long need)
    • Full Archive Premium: Lets you pull tweets from the entire history of Twitter (perfect for your 1-year range)
  • Note: Premium APIs are paid services—pricing depends on your usage, so check the Developer Platform for details on costs and rate limits.

2. Use Tweepy’s Premium Search Methods

Tweepy supports calling these Premium endpoints with dedicated methods, instead of the standard search(). Here’s a quick example tailored to your Python 3.5 environment:

import tweepy

# Replace these with your actual API credentials
consumer_key = "your_consumer_key"
consumer_secret = "your_consumer_secret"
access_token = "your_access_token"
access_token_secret = "your_access_token_secret"

# Authenticate with Twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

# Call the Full Archive Search (requires Full Archive Premium access)
# You'll need to replace 'your_premium_environment' with the environment name you created in the Developer Platform
historical_tweets = api.search_full_archive(
    environment_name="your_premium_environment",
    query="your_search_query",
    fromDate="202301010000",  # Format: YYYYMMDDHHMM (start of your 1-year window)
    toDate="202401010000"     # End of your 1-year window
)

# Iterate through and process the tweets
for tweet in historical_tweets:
    print(f"Tweet from {tweet.created_at}: {tweet.text}")

3. Compatibility with Your Python/Spyder Versions

Your Python 3.5 and Spyder 3.5.6 will work just fine, but you’ll need to install a compatible version of Tweepy. Newer Tweepy versions drop support for Python 3.5, so stick with Tweepy 3.x. Install it with:

pip install tweepy==3.10.0

Quick Notes

  • Make sure the fromDate and toDate follow the strict YYYYMMDDHHMM format—no slashes or hyphens allowed.
  • You’ll need to create a Premium environment in the Twitter Developer Platform first (under the "Products" section) before you can use the search_full_archive() method.
  • If you don’t want to pay for Premium access, some third-party archival tools exist, but always make sure they comply with Twitter’s Developer Agreement to avoid account issues.

内容的提问来源于stack exchange,提问作者Seetha Ramayya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:20:35