InstaPy / InstaPy

📷 Instagram Bot - Tool for automated Instagram interactions
GNU General Public License v3.0
16.72k stars 3.77k forks source link

how to get a list of all my followers ? #707

Closed converge closed 6 years ago

converge commented 6 years ago

is there a way to get a list of all followers my account has ?

I tried using the scroll_down() function, but it only gets a total of 270 followers, and I need all of them.

converge commented 6 years ago

I'm working in this function to unfollow the profile if it's not following me:

https://github.com/converge/InstaPy/blob/master/instapy/instapy.py

(I made some mess at my repo, can't compare/history in a good way)

the method is unfollow_if_not_following_me() , the problem is that I don't know how many times I have to scroll the page scroll_down() until the end of the followers list.

ClimateYukon commented 6 years ago

I don't think that you can scroll down to the end of the list. Another way which I am using at the moment is to use part of this repo https://github.com/LevPasha/Instagram-API-python. The way I do it is saving people who dont follow me as a list and then I created an unfollow by list function. It works pretty well. But the same, my code is now a mess as it has piece of two different tools. The repo I mentionned didn't work right away, I had to change a few things to make it work

In more details I use the getUserFollowers and getUserFollowings of that repo to produce the list of 'fake'.

from instapy import InstaPy
import time
import pickle
from InstagramAPI import InstagramAPI
from datetime import datetime

username = ''
pwd = ''
user_id  = ''

API = InstagramAPI(username,pwd)
API.login()

API.getUsernameInfo(user_id)
API.LastJson
followers   = []
next_max_id = True
while next_max_id:
    if next_max_id == True: next_max_id=''
    _ = API.getUserFollowers(user_id,maxid=next_max_id)
    followers.extend ( API.LastJson.get('users',[]))
    next_max_id = API.LastJson.get('next_max_id','')

user_fer = [dic['username'] for dic in followers]

following   = []
next_max_id = True
while next_max_id:
    if next_max_id == True: next_max_id=''
    _ = API.getUserFollowings(user_id,maxid=next_max_id)
    following.extend ( API.LastJson.get('users',[]))
    next_max_id = API.LastJson.get('next_max_id','')

user_fing = [dic['username'] for dic in following]

fake = [x for x in user_fing if x not in user_fer]
fake.reverse()
print("There are {} fakes remaining".format(len(fake)))
# with open('/log/fake.txt','wb') as fp :
#     pickle.dump(user_ls,fp)
API.logout()`

then my unfollow_user :

def unfollow_user2(browser , acc_to_unfollow , delay):
    """Unfollows the user of the currently opened image"""
    browser.get('https://www.instagram.com/' + acc_to_unfollow)
    print('--> {} instagram account is opened...'.format(acc_to_unfollow))

    try:
        follow_button = browser.find_element_by_xpath(".//*[@id='react-root']/section/main/article/header/div[2]/div[1]/span/span[1]/button")
        sleep(delay)
        follow_button.click()

        print('---> Now unfollowing: {}'.format(acc_to_unfollow))
        print('*' * 20)
        sleep(3)
        return 1
    except NoSuchElementException:
        print('---> {} div issue... skipping'.format(acc_to_unfollow))
        print('*' * 20)
        sleep(3)
        return 0

Sometimes my find_element_by_xpath fail hence the "try"

mattalhonte commented 6 years ago

This has some JavaScript code to get Selenium to scroll to the bottom of a page with dynamically-loading data: https://sqa.stackexchange.com/questions/18796/unable-to-scroll-down-to-bottom-of-div-with-data-loading-dynamically

conradlo commented 6 years ago

Actually it's possible to get full list of followers/following users. The trick is try to avoid hitting Instagram's rate limit Otherwise the browser will get HTTP 429 error and cannot make the same query in the next ~60mins.

I've tried (manually) to scroll the /following & /followers page every 5-10 sec for 30 times (i.e. query 30 times) and then waited couple minutes and continue. This way I can get a full list of followings (>1000).

Just try to slow down and spread out the queries over time.

converge commented 6 years ago

thank you guys, great ideas.

@conradlo I'm trying your advice at the moment, but it take too long to scroll down to the bottom of the page. I have more than 3k followers, at the moment it scrolled down 10% of total followers in 30min.

@mattalhonte I start to thinking that scroll down until the bottom is not a good idea because of many requests required to it.

sionking commented 6 years ago

I am using an application "captivate" for IOS which get all users followers/following immediatly. So it is possiable but not sure how. Maybe instagram API ?

converge commented 6 years ago

@sionking I'm trying to not use 3rd software.

henrytriplette commented 6 years ago

I made a dumb script by extending @julienschroder solution. I have two files, whitelist.txt and followers.txt.

import os
import random
import imageio
imageio.plugins.ffmpeg.download()
from instapy import InstaPy
import time
import numpy
from InstagramAPI import InstagramAPI
from datetime import datetime

username = 'USERNAME'
pwd = 'PASSWORD'
user_id  = 'USERID'

API = InstagramAPI(username,pwd)
API.login()

API.getUsernameInfo(user_id)
API.LastJson
followers   = []
next_max_id = True
while next_max_id:
    if next_max_id == True: next_max_id=''
    _ = API.getUserFollowers(user_id,maxid=next_max_id)
    followers.extend ( API.LastJson.get('users',[]))
    next_max_id = API.LastJson.get('next_max_id','')

user_fer = [dic['username'] for dic in followers]

print("There are {} followers".format(len(followers)))

f = open("followers.txt", 'r+')
f.truncate()
for i in user_fer:
    f.write(i + '\n')

API.logout()

with open('whitelist.txt') as f:
    whitelist = f.read().splitlines()

with open('followers.txt') as f:
    followers = f.read().splitlines()

whitelist_followers = whitelist + list(set(followers) - set(whitelist))
print("There are {} whitelist".format(len(whitelist_followers)))

f = open("whitelist.txt", 'r+')
f.truncate()
for i in whitelist_followers:
    f.write(i + '\n')

I merge my updated followers list with my whitelist.txt, then I import my whitelist in InstaPy

converge commented 6 years ago

still looking for an API free solution. Maybe soon we will have a better path to solve it https://www.engadget.com/2017/09/21/instagram-follows-you-feature-trial-android-ios/

bsoum commented 6 years ago

Hello! With this InstagramAPI getUserFollowers, it's possible to get only public account? Thanks! @henrytriplette @julienschroder

timgrossmann commented 6 years ago

@bsoum I think we simply can get this by implementing #1073 which will give us all the followers followings with a lot of information like if they are private or not

bsoum commented 6 years ago

@timgrossmann Hey thx! It work perfectly!!!! 👌🤗

thebeachtoday commented 6 years ago

@timgrossmann - Great work man! Have you figured out how to use the GraphQL modifiable data endpoint parameter "after"? As I'm sure you've found "somewhere" between 7,000 and 11,000 users bombs out when pulling followers for a given account. If we can figure out what this parameter is, and how to calculated valid values, we should be able to easily get mid-size accounts' followers [e.g. 250,000 followers]. I'm going to be spending a lot of time digging into this and other items this afternoon. Also, not sure if you've updated your TODO list, but I have solved all three - all without using the Instagram API. I can share this with you [just don't have time to document/format everything for public consumption]. Shoot me a note, and we can kick the tires on collaborating to save us both time. SnapChat: thebeachtoday

Tkd-Alex commented 6 years ago

@converge i've made something similar here https://github.com/Tkd-Alex/WhoUnfollowed-Telegram-Bot/blob/master/igsentinel.py

but with account with fw >= 6000 i've found a Ram problem