Scrape Instagram using python
This post talks about how we can connect to Instagram using python and extract list of followers, people whom you follow and the list of people you should unfollow (people whom you follow but they don’t 😛)
We can easily do the above using a built in python package called
instaloader. In case you directly want to jump to code and see it in action here it is - https://github.com/apoorva-dave/instagram-scraper
Create a file
insta_scraper.py which will handle the below 4 steps
- Create a session
We get an instance of Instaloader in below codeblock and login using the username and password provided by the user. Once that is done, profile instance is created in order to fetch the profile’s metadata.
def create_session(self): L = instaloader.Instaloader()
L.login(self.username, self.password) # Login or load session
self.profile = instaloader.Profile.from_username(L.context, self.username) # Obtain profile metadata
2. Get list of followers
def scrape_followers(self): for follower in self.profile.get_followers():
3. Get list of following
def scrape_following(self): for followee in self.profile.get_followees():
4. Get unfollow list
This would generate a
unfollowers_<USERNAME>.txt file in your present directory containing the list of people whom you follow but they don't.
def generate_unfollowers_list(self): unfollow_list = np.setdiff1d(self.following_list, self.followers_list) # unfollow people who are only in following list and not in followers list
print("People to unfollow: ", unfollow_list)
filename = "unfollowers_" + self.username + ".txt"
file = open(filename, "w")
for person in unfollow_list:
file.write(person + "\n")
The code can then be executed from a runner script
main.py which would invoke
create_session() using the username and password of the user. The design has been kept in such a way so as to make sure user's username and password is only needed while creating session post which we can directly invoke APIs
scrape_followers() etc as per the requirement.
Instaloader is a very efficient package. We can do much more using it. Please check the documentation here for more details: https://instaloader.github.io/as-module.html
You can find the entire running code here with a
README to provide steps to run.
This is it for this article. Happy learning!! ❤