Get user data
Tags: #twitter #automation #ifttt #naas_drivers #snippet

Input

Import library

1
import tweepy
2
import pandas as pd
Copied!

How to get API keys?

Variables

1
# API Credentials
2
consumer_key = "XXXXXXXXXXXXXXXXXX"
3
consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Copied!
1
user_list = ["JupyterNaas", "Spotify", "ProjectJupyter"]
Copied!

Model

Authentication

1
try:
2
auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
3
api = tweepy.API(auth)
4
except BaseException as e:
5
print(f"Authentication has been failed due to -{str(e)}")
Copied!

Below function will retrive only the information about the user.

1
def getUserInfo(user_id):
2
3
# Define a pandas dataframe to store the date:
4
user_info_df = pd.DataFrame(columns = ['twitter_id', 'name', 'screen_name', 'description', 'tweet_count', 'friends_count',
5
'followers_count', 'favourites_count', 'verified', 'created_at']
6
)
7
8
# Collect userinformation using get_user
9
for user in user_id:
10
info = api.get_user(user) # Get user information request
11
12
twitter_id = info.id
13
name = info.name
14
screen_name = info.screen_name
15
description = info.description
16
tweet_count = info.statuses_count
17
friends_count = info.friends_count
18
followers_count = info.followers_count
19
favourites_count = info.favourites_count
20
verified = info.verified
21
created_at = info.created_at
22
23
24
user_info = [twitter_id, name, screen_name, description, tweet_count, friends_count,
25
followers_count, favourites_count, verified, created_at]
26
27
user_info_df.loc[len(user_info_df)] = user_info
28
29
30
return user_info_df
Copied!

Output

Get user data

1
df = getUserInfo(user_list)
Copied!
Copy link
Edit on GitHub