I have a list of users, for which I want to extract their followers on Instagram. I am trying to scroll through the follower dialog box and append every follower to a list, so I end up with the full list of followers of a user before I move on to do the same for the next user.
This is my code:
for username in username_column:
currentRow=currentRow+1
userprofile = browser.get('https://www.instagram.com/' + username)
# Find the "followers" link, click it and wait 2 seconds
followersLink = browser.find_element_by_xpath("//ul/li[2]/a")
followersLink.click()
time.sleep(2)
# Find the dialog box
followersList = browser.find_element_by_css_selector("div[role=\'dialog\'] ul")
numberOfFollowersInList = len(followersList.find_elements_by_css_selector('li'))
followersList.click()
actionChain = webdriver.ActionChains(browser)
followers = []
# As long as the number of followers in the list is lower than number required
# press SPACE. After each press, we refresh the number of users we have in the list
# and print it.
while (numberOfFollowersInList < max):
actionChain.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
time.sleep(2)
numberOfFollowersInList = len(followersList.find_elements_by_css_selector('li'))
followersInList = followersList.find_elements_by_css_selector('li')
for element in followersInList:
followerData = element.text
print(followerData)
followers.append(followerData)
#print(followers)
followersList.click()
However, this returns me a lot of duplicate follower data and when I watch the browser (driver) do it on Instagram, it is like it gets stuck, while scrolling and doesn't know where to start scrolling from once it's scrolled once. I hope it's clear what I wrote.
Thank you in advance!