Petr Petrov
Petr Petrov

Reputation: 4442

Parsing followings from Instagram

I have some accounts, and I should get it's followings. I try to use requests

for username in usernames_list:
    url = 'https://www.instagram.com/' + username + '/following/'
    page = requests.get(url).content

And I have thought that I can get page with followings and next parsing names of this pages. But it returns start page and I can't open page of followings. Is any way to parse followings from the instagram?

Upvotes: 0

Views: 2069

Answers (1)

aandergr
aandergr

Reputation: 637

It seems it is required to be logged in to Instagram to obtain this kind of information, which might explain why it is not so easy to get them with one simple JSON query.

The Instaloader package provides a convenient way to login and then programmatically access a profile's followees (followings).

import instaloader

# Get instance
L = instaloader.Instaloader()

# Login or load session
L.login(USER, PASSWORD)        # (login)
L.interactive_login(USER)      # (ask password on terminal)
L.load_session_from_file(USER) # (load session created w/
                               #  `instaloader -l USERNAME`)

# Obtain profile metadata
profile = instaloader.Profile.from_username(L.context, PROFILE)

# Print list of followees
for followee in profile.get_followees():
    print(followee.username)

Besides username, the attributes full_name, userid, followed_by_viewer and many more are defined in the Profile instance that is returned for each followee.

Upvotes: 2

Related Questions