Iterate each user on instagram with specific conditions

旧时模样 提交于 2021-02-11 12:41:56

问题


I am using selenium and im trying to filter a specific users on instagram with 2 condition :

  1. If the profile doesn't have a profile pic (instagram's default pic contains "YW5vbnl..." string)
  2. If i already follow this user

enter image description here

but he doesn't iterate all user, he just iterate 12 times the result of last user however this user is eligible

Simple example:

enter image description here enter image description here

PS : sorry for mistake i am beginner on python 😁

try:

browser.get('https://www.instagram.com/therock/')
sleep(2) 
browser.find_element_by_xpath('//a[contains(@href, "%s")]' % page).click()
scr2 = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a')
sleep(2)
text1 = scr2.text
print(text1)

followers = []

elems = browser.find_elements_by_xpath("//body//div[@class='PZuss']//a[@class='FPmhX notranslate  _0imsa ']")
url = "https://www.instagram.com/"

    
for i in range(12):
       # Trying to generate a list where to put the user URL
        val = elems[i].get_attribute('innerHTML')
        followers.append(url+val)
    
print(followers)
    
for i in followers:
        #Iterate into the list
        browser.get(url+val)
        sleep(2)
        
        followButton = browser.find_element_by_css_selector('button')
        
        if browser.find_elements_by_xpath("//img[contains(@src,'YW5vbnltb3VzX3Byb2ZpbGVfcGlj')]") or (followButton.text != 'Follow'):
            print("You are already following this user or don't have profile pic")
                
        else:
            print("eligible")
            followButton.click()
            
break      
  # except StopIteration:
    # break
    

finally: browser.quit


回答1:


In browser.get(url+val) you are using val, which is defined in the previous for loop, in each iteration. You should change the second for loop in something like

for follower in followers:
        #Iterate into the list
        browser.get(follower)


来源:https://stackoverflow.com/questions/65928857/iterate-each-user-on-instagram-with-specific-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!