0

This is my code:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# loading the webpage
browser = webdriver.Chrome()
browser.get("https://instagram.com")
time.sleep(1)

# finding essential requirements
user_name = browser.find_element_by_name("username")
password = browser.find_element_by_name("password")
login_button = browser.find_element_by_xpath("//button [@type = 'submit']")

# filling out the user name box
user_name.click()
user_name.clear()
user_name.send_keys("username")

# filling out the password box
password.click()
password.clear()
password.send_keys("password")

# clicking on the login button
login_button.click()
time.sleep(3)

# information save permission denial
not_now_button = browser.find_element_by_xpath("//button [@class = 'sqdOP yWX7d    y3zKF     ']")
not_now_button.click()
time.sleep(3)

# notification permission denial
not_now_button_2 = browser.find_element_by_xpath("//button [@class = 'aOOlW   HoLwm ']")
not_now_button_2.click()
time.sleep(3)

# finding search box and searching + going to the page
search_box = browser.find_element_by_xpath('//input [@placeholder="Search"]')
search_box.send_keys("sb else's page")
time.sleep(3)
search_box.send_keys(Keys.RETURN)
search_box.send_keys(Keys.RETURN)
time.sleep(3)

# opening ((followers)) list
followers = browser.find_element_by_xpath('//a [@class="-nal3 "]')
followers.click()
time.sleep(10)

# following each follower
follower = browser.find_elements_by_xpath('//button [@class="sqdOP  L3NKy   y3zKF     "]')


browser.close()

In this code, I normally simulate what a normal person does to follow another person. I want to follow each follower of a page. I have thought all day long; But couldn't find any algorithms.


Got some good ideas, but just realized I don't know how I can scroll down to the end of the list to get the entire list. Can you help? (If you don't get me, try running the code and then extract the list of followers.)

Mani
  • 45
  • 12

2 Answers2

0

First you need to get a list of the users that follows someone, then you just execute the same code in a loop. You can either scrape the users separate, or within selenium. Then run the code needed to follow a given person, in Ex. a for loop. Step 6: profit

Goldwave
  • 599
  • 3
  • 13
  • I think this is a good idea. Just realized I don't know how I can scroll down to the end of the list to get the entire list. Can you help? (If you don't get me, try running the code.) – Mani Oct 03 '20 at 19:01
  • I'm sorry, I dont know how. Also, there is a limit to how many you can follow a day – Goldwave Oct 03 '20 at 19:14
0
# following each follower
  • get list of followers
  • for each follower - click 'follow' if it's possible
  • if button text haven't changed, it means that you reached the limit of follows, or maybe banned

Also, be sure to limit your actions, instagram had limit of follows (30 per hour, it was before) And you can get the followers directly through instagram API. And don't forget to unfollow them, because unfollowing also has limits. And the limit of current follows is 7500( it was before, not sure how about now)

  • Helpful, thanks. But one more thing. I don't know how I should scroll down to the end of followers. Can you help? – Mani Oct 03 '20 at 19:11
  • https://stackoverflow.com/questions/20986631/how-can-i-scroll-a-web-page-using-selenium-webdriver-in-python check this one. Be sure to scroll the element you need instad of scrolling the page;) – Vladislav Abyshkin Oct 03 '20 at 19:17
  • Also, list of followers start to duplicate after some time. Try to get the followers using their API. Not the official instagram API which it offers, but the way how the browser updates the followers list:) – Vladislav Abyshkin Oct 03 '20 at 19:21
  • Sorry, but, would you pls explain what API is? – Mani Oct 03 '20 at 20:23
  • To see how it works, open somebody's page, wait till it loaded full, open developer tools ->> and Network tab. Then click the 'followers' button. Look what happened in network tab. Scroll the followers tab for updating it. Have you noticed that there are some new requests in your network tab? That's how it works. https://pastebin.com/fVrgwR9w - here is an example but in JS. Line 24, get ID of the user. Line 38\40, replace the ID with ID of the acc you want to scrape followers. Go to this page, you'll see some JSON there. Use the json.page_info.end_cursor to go the next page. – Vladislav Abyshkin Oct 03 '20 at 23:35