1

I am trying to create an Instagram scraping bot that collects a list of Followers and Following using Python + Selenium.

However, that list keeps on loading when the user scrolls until the list is exhausted. I am attaching a screenshot for reference (some content hidden due to privacy reasons):

Now, I believe I have two ways to achieve this:

  1. Keep reading the usernames, and then keep on scrolling.
  2. Keep scrolling till the end, and then read all usernames together from the source code.

I've been trying to figure this out using the second method. However, I am not able to figure out how to know when there is no more content to scroll. How can I achieve this (provided that I don't know anything about the length of this element)?

Reason for not using Method 1: When scrolling, the DOM keeps getting refreshed, so it is hard to keep track of which usernames have been read.

1 Answer 1

1

One way to do this is is to keep track of the amount of child elements the div has that contains the li elements for the followers. If it doesn't increase after a scroll event, you've reached the end of the list.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, this crossed my mind. But this would require to check the count of li elements after every scroll. That seemed to be quite a waste of computational power.
I tried some other things as well like scrolling down total_users / users_scrolled_in_one_KEY_DOWN times but that did not work smoothly. So I adapted your idea but changed KEY_DOWN to END signal so there are fewer repetition in calculating the count of li tags. Leaving it out in the comments as it might help someone else.
I was expecting something which could provide me with a signal when scrolling can't be done further. However, this has helped. Accepting this solution for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.