results = await client.search_user('data scientist', count=20)# Get more resultsmore_results = await results.next()for user in more_results: print(f'@{user.screen_name}')
results = await client.search_user('AI researcher', count=50)# Filter users by follower countpopular_users = [u for u in results if u.followers_count > 1000]# Sort by followerssorted_users = sorted(results, key=lambda u: u.followers_count, reverse=True)print('Top 10 users by followers:')for user in sorted_users[:10]: print(f'@{user.screen_name}: {user.followers_count} followers')
# Get trends for a specific location using WOEID# WOEID for New York: 2459115# WOEID for London: 44418# WOEID for Tokyo: 1118370nyc_trends = await client.get_place_trends(2459115)print(f"Trending in {nyc_trends['locations'][0]['name']}:")for trend in nyc_trends['trends']: print(f" {trend.name}") if trend.tweet_volume: print(f" Volume: {trend.tweet_volume} tweets")
locations = await client.get_available_locations()nyc = next(loc for loc in locations if loc.name == 'New York')print(f'Location: {nyc.name}')print(f'Country: {nyc.country}')print(f'WOEID: {nyc.woeid}')# Get trends for this locationtrends = await nyc.get_trends()
tweet = await client.get_tweet_by_id('1234567890')# Get similar tweetssimilar_tweets = await tweet.get_similar_tweets()print(f'Original tweet: {tweet.text}')print(f'\nSimilar tweets:')for similar in similar_tweets: print(f' @{similar.user.screen_name}: {similar.text}')
The get_similar_tweets() method requires Twitter Premium/Blue subscription.
import asyncioasync def monitor_keyword(keyword): seen_tweet_ids = set() while True: try: tweets = await client.search_tweet( query=keyword, product='Latest', count=20 ) for tweet in tweets: if tweet.id not in seen_tweet_ids: print(f'New tweet about {keyword}:') print(f' @{tweet.user.screen_name}: {tweet.text}') seen_tweet_ids.add(tweet.id) # Check every 60 seconds await asyncio.sleep(60) except Exception as e: print(f'Error: {e}') await asyncio.sleep(60)# Monitor a specific keywordawait monitor_keyword('Python')
users = await client.search_user('AI researcher', count=50)# Filter and sort by engagementinfluencers = sorted( [u for u in users if u.followers_count > 5000], key=lambda u: u.followers_count, reverse=True)print('Top AI researcher influencers:')for i, user in enumerate(influencers[:10], 1): print(f'{i}. @{user.screen_name}') print(f' Followers: {user.followers_count:,}') print(f' Tweets: {user.statuses_count:,}') print(f' Bio: {user.description[:100]}...') print()
categories = ['trending', 'news', 'sports', 'entertainment']trends_by_category = {}for category in categories: trends = await client.get_trends(category, count=10) trends_by_category[category] = [t.name for t in trends] print(f'\n{category.upper()} trends:') for trend in trends: print(f' {trend.name}')# Find common trends across categoriescommon_trends = set(trends_by_category['trending'])for category in categories[1:]: common_trends &= set(trends_by_category[category])if common_trends: print(f'\nTrends appearing in all categories: {common_trends}')