Created
February 14, 2018 19:41
-
-
Save uluQulu/0ff50e7b4c3e4ee423a45462d5ef3aab to your computer and use it in GitHub Desktop.
Get all of the active users by scrolling to the bottom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_active_users(browser, username, posts, logger): | |
"""Returns a list with users who liked the latest posts""" | |
browser.get('https://www.instagram.com/' + username) | |
sleep(2) | |
total_posts = formatNumber(browser.find_element_by_xpath( | |
"//span[contains(@class,'_t98z6')]//span").text) | |
# if posts > total user posts, assume total posts | |
if posts >= total_posts: | |
# reaches all user posts | |
posts = total_posts | |
# click latest post | |
browser.find_element_by_xpath( | |
"(//div[contains(@class, '_si7dy')])[1]").click() | |
active_users = [] | |
# posts argument is the number of posts to collect usernames | |
for count in range(0, posts): | |
try: | |
browser.find_element_by_xpath( | |
"//a[contains(@class, '_nzn1h')]").click() | |
dialog = browser.find_element_by_xpath( | |
"//div[text()='Likes']/following-sibling::div") | |
scroll_it = True | |
while scroll_it != False: | |
scroll_it = browser.execute_script(''' | |
var div = arguments[0]; | |
if (div.offsetHeight + div.scrollTop < div.scrollHeight) { | |
div.scrollTop = div.scrollHeight; | |
return true;} | |
else if (div.offsetHeight + div.scrollTop >= div.scrollHeight) { | |
return false;} | |
else { | |
return null;} | |
''', dialog) | |
if scroll_it is None: | |
print ("Too Many Requests!\n\n~sleeping a bit") | |
sleep(427) | |
else: | |
sleep(4.27) | |
tmp_list = browser.find_elements_by_xpath( | |
"//a[contains(@class, '_2g7d5')]") | |
except NoSuchElementException: | |
try: | |
tmp_list = browser.find_elements_by_xpath( | |
"//div[contains(@class, '_3gwk6')]/a") | |
except NoSuchElementException: | |
logger.error('There is some error searching active users') | |
if len(tmp_list) is not 0: | |
for user in tmp_list: | |
active_users.append(user.text) | |
sleep(1) | |
# if not reached posts(parameter) value, continue | |
if count+1 != posts: | |
try: | |
# click next button | |
browser.find_element_by_xpath( | |
"//a[@class='_3a693 coreSpriteRightPaginationArrow']" | |
"[text()='Next']").click() | |
except: | |
logger.error('Unable to go to next profile post') | |
# delete duplicated users | |
active_users = list(set(active_users)) | |
return active_users |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment