可能な限り全てのツイートを検索して取得する。ただし、サーチバン等により検索で出て来ないツイートは取得できない。
import tweepy tweets = [] query = "物理の駅" for page in tweepy.Cursor(api.search_tweets,q=query, tweet_mode='extended',result_type="mixed",lang='ja').pages(): time.sleep(5) for tweet in page: tweets.append(tweet._json)
少数でいいならこういう方法もある
item_number = 10 tweets = tweepy.Cursor(api.search_tweets,q=search_word, tweet_mode='extended',result_type="mixed",lang='ja').items(item_number)
ユーザー情報を取得
user = api.get_user(screen_name='twitter') print(user._json)
結果
{'id': 783214, 'id_str': '783214', 'name': 'Twitter', 'screen_name': 'Twitter', 'location': 'everywhere', 'profile_location': None, 'description': "What's happening?!", 'url': 'https://t.co/DAtOo6uuHk', 'entities': {'url': {'urls': [{'url': 'https://t.co/DAtOo6uuHk', 'expanded_url': 'https://about.twitter.com/', 'display_url': 'about.twitter.com', 'indices': [0, 23]}]}, 'description': {'urls': []}}, 'protected': False, 'followers_count': 65036598, 'friends_count': 5, 'listed_count': 87347, 'created_at': 'Tue Feb 20 14:35:54 +0000 2007', 'favourites_count': 6201, 'utc_offset': None, 'time_zone': None, 'geo_enabled': True, 'verified': True, 'statuses_count': 15043, 'lang': None, 'status': {'created_at': 'Thu Oct 13 21:41:45 +0000 2022', 'id': 1580675180602413057, 'id_str': '1580675180602413057', 'text': '@ElenbaasHier 🥺', 'truncated': False, 'entities': {'hashtags': [], 'symbols': [], 'user_mentions': [{'screen_name': 'ElenbaasHier', 'name': 'Katelijne', 'id': 1551146624294035456, 'id_str': '1551146624294035456', 'indices': [0, 13]}], 'urls': []}, 'source': '<a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>', 'in_reply_to_status_id': 1580665048271577088, 'in_reply_to_status_id_str': '1580665048271577088', 'in_reply_to_user_id': 1551146624294035456, 'in_reply_to_user_id_str': '1551146624294035456', 'in_reply_to_screen_name': 'ElenbaasHier', 'geo': None, 'coordinates': None, 'place': None, 'contributors': None, 'is_quote_status': False, 'retweet_count': 16, 'favorite_count': 1781, 'favorited': False, 'retweeted': False, 'lang': 'und'}, 'contributors_enabled': False, 'is_translator': False, 'is_translation_enabled': False, 'profile_background_color': 'ACDED6', 'profile_background_image_url': 'http://abs.twimg.com/images/themes/theme18/bg.gif', 'profile_background_image_url_https': 'https://abs.twimg.com/images/themes/theme18/bg.gif', 'profile_background_tile': True, 'profile_image_url': 'http://pbs.twimg.com/profile_images/1488548719062654976/u6qfBBkF_normal.jpg', 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/1488548719062654976/u6qfBBkF_normal.jpg', 'profile_banner_url': 'https://pbs.twimg.com/profile_banners/783214/1646075315', 'profile_link_color': '1B95E0', 'profile_sidebar_border_color': 'FFFFFF', 'profile_sidebar_fill_color': 'F6F6F6', 'profile_text_color': '333333', 'profile_use_background_image': True, 'has_extended_profile': True, 'default_profile': False, 'default_profile_image': False, 'following': False, 'follow_request_sent': False, 'notifications': False, 'translator_type': 'regular', 'withheld_in_countries': []}
フォローしてるユーザーを取得
for friend in user.friends(): print(friend.screen_name)
フォロワーを取得
count = 500 followers = [] for page in tweepy.Cursor(api.get_followers, screen_name="twitter",count=200).pages(): followers += [user._json for user in page] print("取得数",len(followers)) if len(followers)>count:break
ホームタイムラインを40個取得
count=40 tweets = [] max_id = None while True: home_timeline = api.home_timeline(max_id=max_id) tweets += [tweet._json for tweet in home_timeline] print(len(tweets)) max_id=tweets[-1]["id"]-1 if len(tweets)>=count:break for tweet in tweets: print(tweet["created_at"],tweet["user"]["name"],"@",tweet["user"]["screen_name"])
メンションを40個取得
count=40 tweets = [] max_id = None while True: mentions_timeline = api.mentions_timeline(max_id=max_id) tweets += [tweet._json for tweet in mentions_timeline] print(len(tweets)) max_id=tweets[-1]["id"]-1 if len(tweets)>=count:break for tweet in tweets: print(tweet["created_at"],"https://twitter.com/{}/status/{}".format(tweet["user"]["screen_name"],tweet["id"]))