I will summarize how to use tweepy with reference to the official document.
Advance preparation
import tweepy
#Obtained API key
consumer_key = '*********************************'
consumer_secret = '*************************************'
access_token = '*******************************************'
access_token_secret = '****************************************'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
The User type of your account is returned by ʻapi.me ()as shown below. Looking at the returned value, it looks like
'followers_count': 112, so you can get the value by doing ʻapi.me (). Followers_count
.
By looking at this dict type-like User type key, you can get other information as well.
api.me()
#It's long and can be ignored.
>> User(_api=<tweepy.api.API object at 0x7efcf29adc50>, _json={'id': 1147865430297468928, 'id_str': '1147865430297468928', 'name': 'Kita', 'screen_name': 'ki_rii9', 'location': 'Fukuoka, Japan', 'profile_location': None, 'description': 'python/Machine learning/ flask/ Atcoder/Beginner Competitive Pro er Beginner kaggler/Kyushu University B1/ GLEAP #TORYUMON #Chare character', 'url': 'https://t.co/6QXllpbNqu', 'entities': {'url': {'urls': [{'url': 'https://t.co/6QXllpbNqu', 'expanded_url': 'https://qiita.com/kitarikes', 'display_url': 'qiita.com/kitarikes', 'indices': [0, 23]}]}, 'description': {'urls': []}}, 'protected': False, 'followers_count': 112, 'friends_count': 268, 'listed_count': 1, 'created_at': 'Sun Jul 07 13:50:19 +0000 2019', 'favourites_count': 807, 'utc_offset': None, 'time_zone': None, 'geo_enabled': False, 'verified': False, 'statuses_count': 135, 'lang': None, 'status': {'created_at': 'Thu Dec 26 17:50:38 +0000 2019', 'id': 1210256620657102849, 'id_str': '1210256620657102849', 'text': 'Completed because React was added without knowing! !\n You have completed the "React Learning Course IV" course! https://t.co/dAKcZhNNMS #Progate', 'truncated': False, 'entities': {'hashtags': [{'text': 'Progate', 'indices': [79, 87]}], 'symbols': [], 'user_mentions': [], 'urls': [{'url': 'https://t.co/dAKcZhNNMS', 'expanded_url': 'https://prog-8.com/lessons/react/study/4', 'display_url': 'prog-8.com/lessons/react/…', 'indices': [55, 78]}]}, 'source': '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>', 'in_reply_to_status_id': None, 'in_reply_to_status_id_str': None, 'in_reply_to_user_id': None, 'in_reply_to_user_id_str': None, 'in_reply_to_screen_name': None, 'geo': None, 'coordinates': None, 'place': None, 'contributors': None, 'is_quote_status': False, 'retweet_count': 0, 'favorite_count': 6, 'favorited': False, 'retweeted': False, 'possibly_sensitive': True, 'lang': 'ja'}, 'contributors_enabled': False, 'is_translator': False, 'is_translation_enabled': False, 'profile_background_color': 'F5F8FA', 'profile_background_image_url': None, 'profile_background_image_url_https': None, 'profile_background_tile': False, 'profile_image_url': 'http://pbs.twimg.com/profile_images/1189529639242223622/YrkeODbb_normal.jpg', 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/1189529639242223622/YrkeODbb_normal.jpg', 'profile_banner_url': 'https://pbs.twimg.com/profile_banners/1147865430297468928/1570725567', 'profile_link_color': '1DA1F2', 'profile_sidebar_border_color': 'C0DEED', 'profile_sidebar_fill_color': 'DDEEF6', 'profile_text_color': '333333', 'profile_use_background_image': True, 'has_extended_profile': True, 'default_profile': True, 'default_profile_image': False, 'can_media_tag': True, 'followed_by': False, 'following': False, 'follow_request_sent': False, 'notifications': False, 'translator_type': 'none', 'suspended': False, 'needs_phone_verification': False}, id=1147865430297468928, id_str='1147865430297468928', name='Kita', screen_name='ki_rii9', location='Fukuoka, Japan', profile_location=None, description='python/Machine learning/ flask/ Atcoder/Beginner Competitive Pro er Beginner kaggler/Kyushu University B1/ GLEAP #TORYUMON #Chare character', url='https://t.co/6QXllpbNqu', entities={'url': {'urls': [{'url': 'https://t.co/6QXllpbNqu', 'expanded_url': 'https://qiita.com/kitarikes', 'display_url': 'qiita.com/kitarikes', 'indices': [0, 23]}]}, 'description': {'urls': []}}, protected=False, followers_count=112, friends_count=268, listed_count=1, created_at=datetime.datetime(2019, 7, 7, 13, 50, 19), favourites_count=807, utc_offset=None, time_zone=None, geo_enabled=False, verified=False, statuses_count=135, lang=None, status=Status(_api=<tweepy.api.API object at 0x7efcf29adc50>, _json={'created_at': 'Thu Dec 26 17:50:38 +0000 2019', 'id': 1210256620657102849, 'id_str': '1210256620657102849', 'text': 'Completed because React was added without knowing! !\n You have completed the "React Learning Course IV" course! https://t.co/dAKcZhNNMS #Progate', 'truncated': False, 'entities': {'hashtags': [{'text': 'Progate', 'indices': [79, 87]}], 'symbols': [], 'user_mentions': [], 'urls': [{'url': 'https://t.co/dAKcZhNNMS', 'expanded_url': 'https://prog-8.com/lessons/react/study/4', 'display_url': 'prog-8.com/lessons/react/…', 'indices': [55, 78]}]}, 'source': '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>', 'in_reply_to_status_id': None, 'in_reply_to_status_id_str': None, 'in_reply_to_user_id': None, 'in_reply_to_user_id_str': None, 'in_reply_to_screen_name': None, 'geo': None, 'coordinates': None, 'place': None, 'contributors': None, 'is_quote_status': False, 'retweet_count': 0, 'favorite_count': 6, 'favorited': False, 'retweeted': False, 'possibly_sensitive': True, 'lang': 'ja'}, created_at=datetime.datetime(2019, 12, 26, 17, 50, 38), id=1210256620657102849, id_str='1210256620657102849', text='Completed because React was added without knowing! !\n You have completed the "React Learning Course IV" course! https://t.co/dAKcZhNNMS #Progate', truncated=False, entities={'hashtags': [{'text': 'Progate', 'indices': [79, 87]}], 'symbols': [], 'user_mentions': [], 'urls': [{'url': 'https://t.co/dAKcZhNNMS', 'expanded_url': 'https://prog-8.com/lessons/react/study/4', 'display_url': 'prog-8.com/lessons/react/…', 'indices': [55, 78]}]}, source='Twitter Web Client', source_url='http://twitter.com', in_reply_to_status_id=None, in_reply_to_status_id_str=None, in_reply_to_user_id=None, in_reply_to_user_id_str=None, in_reply_to_screen_name=None, geo=None, coordinates=None, place=None, contributors=None, is_quote_status=False, retweet_count=0, favorite_count=6, favorited=False, retweeted=False, possibly_sensitive=True, lang='ja'), contributors_enabled=False, is_translator=False, is_translation_enabled=False, profile_background_color='F5F8FA', profile_background_image_url=None, profile_background_image_url_https=None, profile_background_tile=False, profile_image_url='http://pbs.twimg.com/profile_images/1189529639242223622/YrkeODbb_normal.jpg', profile_image_url_https='https://pbs.twimg.com/profile_images/1189529639242223622/YrkeODbb_normal.jpg', profile_banner_url='https://pbs.twimg.com/profile_banners/1147865430297468928/1570725567', profile_link_color='1DA1F2', profile_sidebar_border_color='C0DEED', profile_sidebar_fill_color='DDEEF6', profile_text_color='333333', profile_use_background_image=True, has_extended_profile=True, default_profile=True, default_profile_image=False, can_media_tag=True, followed_by=False, following=False, follow_request_sent=False, notifications=False, translator_type='none', suspended=False, needs_phone_verification=False)
#Example
api.me().followers_count
#Check the number of followers
>> 112
api.me().friends_count
#Check the number of followers
>> 268
api.me().description
#Profile comment display
>> 'python/Machine learning/ flask/ Atcoder/Beginner Competitive Pro er Beginner kaggler/Kyushu University B1/ GLEAP #TORYUMON #Chare character'
You can get information about the users you are following with ʻapi.friends (). By default, you can get 20 items, but you can specify the maximum value by setting
count = 100` as an argument.
len(api.friends())
#Get a list of user types of users you are following
>> 20
len(api.friends(count=100))
>> 100
Since this returns a list type, you can perform loop processing using the for statement as it is.
for user in api.friends(count=100):
if user.followed_by :
#Show only users who are following back
print(user.name)
len([user.name for user in api.friends(count=100) if user.followed_by])
>> 40
It's uncomfortable to display the name with user.name, so just the number with len () ...... ~~ (40/100) ~~
You can follow it with ʻapi.create_friendship ()`. It can be specified by id (a unique id that can be obtained by User type .id) or screen_name (@ or less) as an argument. In the example below, we will follow the official account of qiita.
api.create_friendship(screen_name='Qiita')
Similarly, you can unfollow with ʻapi.destroy_friendship (id / screen_name / user_id), and you can see the follow relationship between accounts with ʻapi.show_friendship (source_id / source_screen_name, target_id / target_screen_name)
. (For details, see Documentation)
You can see the tweets you liked with ʻapi.favorites ()`. By default, the likes information of your account is returned, and as before, specify the user with id and screen_name, and specify the maximum number to get with count.
#Example
api.favorites(screen_name='ki_rii9', count=50)
#Caution:This is my account. Thank you for following me.(?)
Like ʻapi.create_favorite (id)`! I can do it. As a test, I'll like the tweet at the top of the timeline.
api.create_favorite(id=api.home_timeline()[0].id)
It can also be canceled with ʻapi.destroy_favorite (id)`.
Like to follow and tweet using API! Can now be done. Next time (if there is next time ...) I will try to increase followers using automatic likes and followers.
Recommended Posts