简体   繁体   中英

How do you automate scrolling Instagram DMs?

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 reasons在此处输入图片说明

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.

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

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM