It is important here to see that the screen_name does not include the do this in the opposite direction, and gather the screen name of an account for whom we know the id, it is as easy as: user = api.get_user(747807250819981312) print(user.screen_name)
This block, which queries the REST API for the user_id of Theresa May’s official Twitter acount, returns: 747807250819981312, which is the id associated which such account. Once we have done this, going from screen name to id and vice versa is very simple, and it is done by running the lines in the following block of code: user = api.get_user(screen_name = 'theresa_may') print(user.id) Notice how this time we do not create a stream object, like we did to use the streaming API, but an api object.
In the code, replace “ENTER YOUR ….” with your credentials, and then run the three last lines to create a connection to the Twitter REST API. import tweepy import time access_token = "ENTER YOUR ACCESS TOKEN" access_token_secret = "ENTER YOUR ACCESS TOKEN SECRET" consumer_key = "ENTER YOUR CONSUMER KEY" consumer_secret = "ENTER YOUR CONSUMER SECRET" auth = tweepy.OAuthHandler(consumer_key, consumer_secret) t_access_token(access_token, access_token_secret) api = tweepy.API(auth)
Going from the Twitter Id to the user Screen name is needed as some of the functions that we will describe later return the Twitter identifier instead of the user screen names, so we need this functionality if we want to see who are the actual users associated with the corresponding ids.Īs always the first step is to collect to the Twitter API. Obtaining Twitter’s Id with the screen name and vice versa
In the data collection process, when we want to specify the user that we want to collect data from, we can either do it using the screen_name or the user_id of such user, so before diving into the more complex functions that are provided by the REST API, we will look at the how to obtain the Twitter Id of a certain user for whom we have the username and vice versa. For example The user_id, which is an unique numerical identifier for each Twitter user, which is a very long numerical string, like 747807250819981312 for example.