如何在Tumblr API中使用多个博客名称/命令行参数
Hey there! I remember that script you've been relying on to fetch over 20 Tumblr posts—glad it's been working smoothly! Now that you need to scale this to hundreds of blogs, let's tweak the script to cut out the manual input hassle and automate the whole process. Here's how to do it:
Step 1: Prepare your blog list
First, create a plain text file (name it something like tumblr_blogs.txt) where each line contains one Tumblr blog name (skip the .tumblr.com suffix unless your original script expects it). For example:
retrofilmdiaries cozybooknook vintagetechcollector
Step 2: Modify the script to read from the file
Take the original script you've been using, and update it to loop through every blog in your text file instead of prompting for manual input. Here's a Python example (assuming your original script was written in Python):
import requests import time import json # Your Tumblr API credentials (keep these secure!) API_KEY = "your_api_key_here" API_ENDPOINT = "https://api.tumblr.com/v2/blog/{}.tumblr.com/posts" def fetch_all_blog_posts(blog_name): all_posts = [] offset = 0 per_request_limit = 20 # Max allowed by Tumblr API per call while True: response = requests.get( API_ENDPOINT.format(blog_name), params={ "api_key": API_KEY, "limit": per_request_limit, "offset": offset } ) if response.status_code != 200: print(f"⚠️ Failed to fetch posts for {blog_name}: {response.text}") break response_data = response.json() batch_posts = response_data.get("response", {}).get("posts", []) if not batch_posts: break # No more posts left to fetch all_posts.extend(batch_posts) offset += per_request_limit time.sleep(1) # Respect Tumblr's rate limits (adjust if needed) # Save posts to a unique file for each blog with open(f"{blog_name}_posts.json", "w", encoding="utf-8") as output_file: json.dump(all_posts, output_file, indent=2, ensure_ascii=False) print(f"✅ Saved {len(all_posts)} posts for {blog_name}") # Read the list of blogs from your text file with open("tumblr_blogs.txt", "r", encoding="utf-8") as blog_list_file: # Filter out empty lines blog_names = [line.strip() for line in blog_list_file if line.strip()] # Process each blog in the list for blog in blog_names: fetch_all_blog_posts(blog)
Step 3: Key tips for smooth bulk processing
- Rate Limit Respect: Tumblr's API has strict rate limits—adding a
time.sleep(1)between requests prevents you from getting blocked. Check their official docs if you need to adjust this timing. - Error Resilience: The script above skips problematic blogs and prints an error message, so one failed blog won't derail the entire batch.
- Organized Storage: Saving each blog's posts to its own JSON file keeps your data tidy and easy to sort through later.
- Credential Security: Avoid hardcoding your API key in public scripts—consider using environment variables to keep it safe if you share the code with others.
This setup should let you process all your blogs automatically without any manual input. Let me know if you need help adapting this to your specific script language or edge cases!
内容的提问来源于stack exchange,提问作者Izzy

