This content originally appeared on DEV Community and was authored by Apoorva Dave
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
Dependencies
- Python3
- Instaloader
- Numpy
Code
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
Get list of followers
def scrape_followers(self):
for follower in self.profile.get_followers():
self.followers_list.append(follower.username)
Get list of following
def scrape_following(self):
for followee in self.profile.get_followees():
self.following_list.append(followee.username)
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")
file.close()
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!! <3
This content originally appeared on DEV Community and was authored by Apoorva Dave
Apoorva Dave | Sciencx (2021-06-13T10:21:47+00:00) Scrape Instagram using python. Retrieved from https://www.scien.cx/2021/06/13/scrape-instagram-using-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.