Here’s the simplest way to download all media from a tweet:
import asynciofrom twikit import Clientclient = Client('en-US')async def main(): await client.login( auth_info_1='USERNAME', auth_info_2='EMAIL', password='PASSWORD' ) # Get a tweet by ID tweet = await client.get_tweet_by_id('1234567890') # Download all media for i, media in enumerate(tweet.media): if media.type == 'photo': await media.download(f'media_{i}.jpg')asyncio.run(main())
Tweets can contain three types of media: photos, videos, and animated GIFs. Here’s how to handle each type:
import asynciofrom twikit import Clientclient = Client('en-US')async def main(): tweet = await client.get_tweet_by_id('...') for i, media in enumerate(tweet.media): if media.type == 'photo': await media.download(f'photo_{i}.jpg')asyncio.run(main())
For videos and GIFs, media.streams[-1] gives you the highest quality stream. Use media.streams[0] for the lowest quality if you want smaller file sizes.
Here’s the actual code from Twikit’s examples that handles all media types:
import asynciofrom twikit import ClientAUTH_INFO_1 = '...'AUTH_INFO_2 = '...'PASSWORD = '...'client = Client('en-US')async def main(): # Login await client.login( auth_info_1=AUTH_INFO_1, auth_info_2=AUTH_INFO_2, password=PASSWORD ) # Get tweet tweet = await client.get_tweet_by_id('...') # Download all media from the tweet for i, media in enumerate(tweet.media): if media.type == 'photo': await media.download(f'media_{i}.jpg') if media.type == 'animated_gif': await media.streams[-1].download(f'media_{i}.mp4') if media.type == 'video': await media.streams[-1].download(f'media_{i}.mp4')asyncio.run(main())
import asynciofrom twikit import Clientclient = Client('en-US')async def download_tweet_media(tweet_id: str, output_dir: str): """Download all media from a specific tweet.""" tweet = await client.get_tweet_by_id(tweet_id) if not tweet.media: print(f'No media found in tweet {tweet_id}') return for i, media in enumerate(tweet.media): filename = f'{output_dir}/tweet_{tweet_id}_media_{i}' if media.type == 'photo': await media.download(f'{filename}.jpg') elif media.type in ['video', 'animated_gif']: await media.streams[-1].download(f'{filename}.mp4') print(f'Downloaded media {i+1}/{len(tweet.media)} from tweet {tweet_id}')async def main(): await client.login( auth_info_1='USERNAME', auth_info_2='EMAIL', password='PASSWORD' ) tweet_ids = ['1234567890', '0987654321', '1111111111'] for tweet_id in tweet_ids: await download_tweet_media(tweet_id, './downloads') await asyncio.sleep(1) # Rate limitingasyncio.run(main())
When downloading media from multiple tweets, add delays between requests to respect rate limits and avoid being temporarily restricted.
Not all tweets contain media. Always check before attempting downloads:
import asynciofrom twikit import Clientclient = Client('en-US')async def main(): await client.login( auth_info_1='USERNAME', auth_info_2='EMAIL', password='PASSWORD' ) tweet = await client.get_tweet_by_id('1234567890') # Check if tweet has media if not tweet.media: print('This tweet has no media') return print(f'Found {len(tweet.media)} media items') # Check media types for i, media in enumerate(tweet.media): print(f'Media {i}: {media.type}') if media.type == 'photo': await media.download(f'media_{i}.jpg') elif media.type in ['video', 'animated_gif']: await media.streams[-1].download(f'media_{i}.mp4')asyncio.run(main())