0

do you have any idea why it unfollow only 15 people instead of all?

You need an account at Instagram to run this script.

for example:
if your account is chrome1 than this script will be working on
https://www.instagram.com/chrome1/following/

for ns in driver.find_elements_by_class_name("_6jvgy"):
    try:
        ns.find_element_by_class_name("_r4e4p").click() # unFollow button!!!

        # time.sleep(2) # the same as without sleep  

        unfollow_nick = ns.find_element_by_class_name("notranslate").get_attribute("title")
        print(unfollow_nick) # now: prints all, but really unfollow only 15. 

    except:
        pass

this question is a continuation of the previous: Scroll in Selenium Webdriver (Python)

all the code needed to run: https://ideone.com/wYjHW4

Community
  • 1
  • 1
lvcpp
  • 169
  • 1
  • 3
  • 16
  • Are you sure all the users loaded before calling this method? As I mentioned in the previous post, It is AJAX request which is loading all the users `10 at a time`, when you scroll down or entered END key. – Naveen Kumar R B Dec 27 '16 at 12:30
  • yes, because: 1) before that loop I run recursiveFunc(1), to scroll the list to the end. 2) prints the names of all the users from the list (~ 40). – lvcpp Dec 27 '16 at 12:38
  • 3) I see it, because use Chrome driver instead of PhantomJs. – lvcpp Dec 27 '16 at 12:39

2 Answers2

0

It is the speed of requests (clicking continuously on Following button) that causing Instagram server to reject/ignore the majority of requests.

Add some sleep time before each request.

In your code, you imported sleep method. so, directly use sleep(2) but not time.sleep, which throws the exception and you are capturing it & bypassed it using pass keyword, so you are not aware of this exception. I suggest print the exception and then use pass keyword.

for ns in driver.find_elements_by_class_name("_6jvgy"):
    try:
        ns.find_element_by_class_name("_r4e4p").click() # unFollow button!!!

        sleep(2) # works now 

        unfollow_nick = ns.find_element_by_class_name("notranslate").get_attribute("title")
        print(unfollow_nick) # now: prints all, but really unfollow only 15. 

    except:
        pass
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • 1
    thanks, I also thought this is a cause. I agree about sleep/time.sleep. I've really imported from time import sleep. Yes, 'pass' is not good to the exception handling. – lvcpp Dec 27 '16 at 14:47
  • but it hasn't fixed the issue. When I do it manually it's as fast as without sleep and works as expected. – lvcpp Dec 27 '16 at 14:52
  • script: without sleep: 15 people; sleep(2) # 15; sleep(8) # 15. – lvcpp Dec 27 '16 at 14:53
  • p.s. I have a fast and stable internet (100 Mbps) and good ping. – lvcpp Dec 27 '16 at 14:53
  • can you see all the users following button is clicked to unfollow them? Give sleep and observe the behaviour, whether unfollow button is clicked for all following users? – Naveen Kumar R B Dec 27 '16 at 15:07
  • yes, it have clicked unfollow button for each user. I've increased sleep after 13rd user. I try to open Developer Tools at Chromium (f12 or Ctrl+Shift+I) but driver closes the tools instantly. – lvcpp Dec 27 '16 at 21:18
  • Then it might be the server. Try manually unfollowing more than 15 users in a shirt time, say 20. Check whether all users selected, are unfollowed. Otherwise, I don't see any logic here. – Naveen Kumar R B Dec 28 '16 at 04:41
  • Yes, this is the server. After 15 requests server send a message, something like (in my profile language): "Too many requests. Please try later.0". And the status code become 403 instead of 200. – lvcpp Dec 30 '16 at 16:08
  • I've checked: after 450 seconds(7.5 min) I can send another 15 requests. Sometimes need to wait even more. – lvcpp Dec 30 '16 at 16:09
0
self.driver.implicitly_wait(10)
        scroll_boX = self.driver.find_element_by_xpath("/html/body/div[5]/div/div/div[2]/div")
        last_ht, ht = 0, 1
        #last_bt, bt = 1, 1
        while last_ht != ht:
            last_ht = ht
           # last_bt = bt
            sleep(2) 
            ht = self.driver.execute_script("""
                arguments[0].scrollTo(0, arguments[0].scrollHeight); 
                return arguments[0].scrollHeight;
                """, scroll_box)
            sleep(2)
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107