Python代码报错TypeError及Tweepy导出空CSV问题求助
Hey there! Let's work through your Tweepy issues step by step—these are super common pitfalls, so you’re not stuck alone here.
1. Fixing the TypeError: a bytes-like object is required, not 'str'
This error almost always comes down to how you’re opening your CSV file. If you’re using binary mode (wb) to write to the file but passing string data (which Tweepy returns for tweets), Python throws this mismatch error.
Quick Fix:
- Open your CSV file in text mode (
w) instead of binary mode, and explicitly set the encoding toutf-8to handle special characters in tweets. Also addnewline=''to avoid extra blank rows in your CSV. - Remove any unnecessary
.encode('utf-8')calls on your tweet text unless you specifically need binary data (you don’t for CSV).
2. Fixing the Empty CSV File
If your code says tweets are downloaded but the CSV is empty, there are a few likely culprits:
Possible Causes & Fixes:
Your
alltweetslist is actually empty:- Double-check the
screen_nameyou’re passing—make sure it’s correct (no typos, and the account isn’t private, unless you have permission to access it). - Ensure your pagination logic is working. Tweepy’s
user_timelineonly returns up to 200 tweets per call, so you need to loop withmax_idto get older tweets. - Add
wait_on_rate_limit=Truewhen initializing the Tweepy API to avoid hitting rate limits silently (this makes Tweepy automatically wait when you hit a limit instead of failing).
- Double-check the
You’re not writing data correctly to the CSV:
- Make sure you’re extracting specific tweet fields (like
id_str,created_at,text) and formatting them as lists/tuples for the CSV writer. - Always write a header row first, then use
writerows()to dump all your tweet data at once (orwriterow()for individual entries).
- Make sure you’re extracting specific tweet fields (like
Full Corrected Code Example
Here’s a revised version of your code that addresses both issues:
import tweepy import csv # Replace these with your actual API credentials consumer_key = "YOUR_CONSUMER_KEY" consumer_secret = "YOUR_CONSUMER_SECRET" access_key = "YOUR_ACCESS_KEY" access_secret = "YOUR_ACCESS_SECRET" def get_all_tweets(screen_name): # Initialize auth and API with rate limit handling auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_key, access_secret) api = tweepy.API(auth, wait_on_rate_limit=True) alltweets = [] # Fetch first batch of tweets new_tweets = api.user_timeline(screen_name=screen_name, count=200) alltweets.extend(new_tweets) # Get the oldest tweet ID to paginate backwards oldest = alltweets[-1].id - 1 if alltweets else None # Keep fetching until no more tweets are available while len(new_tweets) > 0: print(f"Fetching tweets before ID: {oldest}") new_tweets = api.user_timeline(screen_name=screen_name, count=200, max_id=oldest) alltweets.extend(new_tweets) if alltweets: oldest = alltweets[-1].id - 1 print(f"Total tweets downloaded so far: {len(alltweets)}") # Format tweets into rows for CSV outtweets = [ [tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets ] # Write to CSV in text mode with proper encoding with open(f"{screen_name}_tweets.csv", "w", newline="", encoding="utf-8") as f: writer = csv.writer(f) writer.writerow(["Tweet ID", "Created At", "Tweet Text"]) # Header row writer.writerows(outtweets) print(f"Success! Saved {len(alltweets)} tweets to {screen_name}_tweets.csv") # Call the function with your target username get_all_tweets("your_target_username")
Additional Checks:
- Verify your Twitter API credentials have the correct permissions (if you’re accessing private accounts, you need OAuth access for that).
- If you’re using Twitter API v2, you’ll need to adjust the code to use Tweepy’s v2 methods (like
api.get_users_tweets()instead ofuser_timeline), but the above works for v1.1 which is still widely used.
内容的提问来源于stack exchange,提问作者ADITYA RAJAK
相关产品推荐
相关产品推荐

