Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_user_followers(username):
- """
- Retrieves data on all followers of targetted account.
- WARNING: There may be hundreds of thousands or millions of followers
- Be sure you want this data before using this function!
- """
- import pdb; pdb.set_trace()
- api = oauth_authenticate()
- datestamp = str(datetime.datetime.now().strftime("%Y-%m-%d"))
- target = api.get_user(screen_name = username)
- target_id = target.id
- next_cursor = -1
- index = 0
- followersdata = {}
- while next_cursor:
- try:
- for x in limit_handled(tweepy.Cursor(api.followers, screen_name = username).items()):
- followersdata[index] = {}
- followersdata[index]['screen_name'] = x._json['screen_name']
- followersdata[index]['id_str'] = x._json['id_str']
- followersdata[index]['name'] = x._json['name']
- followersdata[index]['description'] = x._json['description']
- followersdata[index]['targeted_user_id'] = target_id
- index = index + 1
- next_cursor = x._json["next_cursor"]
- except (TweepError, ConnectionError) as exc:
- print(exc)
- print("Waiting 15 minutes for next API window to open")
- print("Current Time is:", time.strftime("%I:%M:%S"))
- time.sleep(60*15)
- del api
- api = oauth_authenticate()
- continue
- followersDF = pd.DataFrame.from_dict(followersdata, orient = "index")
- followersDF.to_csv("%s-%s followers list.csv" % (username, datestamp),
- index = False, encoding = 'latin-1')
Add Comment
Please, Sign In to add comment