Wilfred Hawkins
Wilfred Hawkins

Reputation: 11

Python instaloader get max number of followers

Is there a way to limit the number of followers collected by pythons instaloader.get_followers()? I'm trying to download a few of the followers of a 100k+ profile but it times out as there are to many users to download and it tries to download all of them.

    username = "user"
    pw = "pw"

    instaL = instaloader.Instaloader()
    instaL.login(username, pw)

    def get_names(website, follow):
        profile = instaloader.Profile.from_username(instaL.context, website)
        followers = []
        if follow == "followers":
            followers = set(profile.get_followers())
        if follow == "followee":
            followers = set(profile.get_followees())
        names = []
        for follower in followers:
            names.append(follower.username)
        return names

So if I set website as e.g. "world_record_egg" it will just timeout even if I just want the first 100 names

Upvotes: 1

Views: 1696

Answers (1)

aandergr
aandergr

Reputation: 637

There is the itertools.islice() function, which allows to only get the first N elements returned by an iterator. So, to get only the first 100 followers instead of all of them, simply do

from itertools import islice

followers = set(islice(profile.get_followers(), 100))

Upvotes: 1

Related Questions