I am trying to fetch all the followers of a user in python using instaloader package. I can fetch around 30 thousand usernames from one at a time, and after that the provided account got banned by Instagram. By running the script for the first time, I got 33000 usernames and then the script stopped working as the account got banned. So by running the script again, with a different account, I want to continue the scraping from the last retrieved username stored in the .txt file (mentioned in the code).
The purpose of it is to save time and resources as well. Is there any way to do this?
Here is my code:
import instaloader
from datetime import datetime
from itertools import dropwhile, takewhile
import csv
class GetInstagramProfile():
def __init__(self) -> None:
self.L = instaloader.Instaloader()
def get_followers(self,user_name):
'''Note: login required to get a profile's followers.'''
USER = "user"
PASSWORD = "password"
self.L.login(USER, PASSWORD)
profile = instaloader.Profile.from_username(self.L.context, user_name)
existing_usernames = set()
try:
# Read existing usernames from file
with open("ilirlatifi_followers.txt", "r") as file:
for line in file:
existing_usernames.add(line.strip())
except FileNotFoundError:
pass # File does not exist yet, so ignore
# Append new usernames to file
with open("ilirlatifi_followers.txt","a") as file:
print("<------Writing usernames to ilirlatifi_followers.txt------>")
for username in profile.get_followers():
username = username.username
if username not in existing_usernames:
file.write(username + "\n")
if __name__=="__main__":
cls = GetInstagramProfile()
cls.get_followers("ilirlatifi")