4

I'm trying to scrape my Instagram DMs (direct messages). However, I don't know how to automate scrolling down the column to get the additional usernames that messaged me. I've tried this, as well as the scrolling code listed here.

However, neither work because Instagram's DM page is broken into columns. The full page itself does not need to be scrolled. Just the first column. For reference, here's a screenshot of what the Instagram DM page looks like. I blocked out my username and the usernames of people who DMed me for privacy reasonsenter image description here

Does anyone have any ideas on how I can modify the script that scrolls down the page to accommodate the column size?

Thank you for taking the time to read my question and help in any way you can.

shorttriptomars
  • 325
  • 1
  • 9

2 Answers2

2

Instead of targeting the window you should target the messages box element to scroll:

scroll_y = 1000
driver.execute_script(f"document.getElementsByClassName('N9abW')[0].scrollTo(0,{scroll_y})")

if it doesn't work with class name try with Xpath:

path = "//*[@id='react-root']/section/div/div[2]/div/div/div[1]/div[2]/div/div/div"
script = f"document.evaluate({path}, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.scrollTo(0,{scroll_y});"
driver.execute_script(script)

Adjust scroll if needed

Pedro Maia
  • 2,666
  • 1
  • 5
  • 20
  • Thank you so much for taking the time to help me! Your solution worked partly. I have to put it in a loop because it doesn't scroll all the way to the bottom of the box immediately. I found that if I put it in a `for loop` and increment to 5, it works. The only problem is I get duplicate answers. – shorttriptomars Oct 27 '21 at 22:29
2

You should instead try to use the export data feature on Instagram, you will get the JSON files of all the messages and media you have sent or received. This will make your task a lot easier.

https://help.instagram.com/contact/505535973176353

https://www.instagram.com/download/request/

Kaushal Sharma
  • 1,770
  • 13
  • 16
  • 1
    Thank you so much for taking the time to answer my question. You're right, getting a JSON file of my data makes it so much easier. – shorttriptomars Oct 27 '21 at 22:29