您如何自动滚动 Instagram DM?

How do you automate scrolling Instagram DMs?

我正在尝试抓取我的 Instagram 私信(私信)。但是,我不知道如何自动向下滚动该列以获取向我发送消息的其他用户名。 我试过了 this, as well as the scrolling code listed here.

但是,两者都不起作用,因为 Instagram 的 DM 页面被分成几列。整个页面本身不需要滚动。只是第一列。作为参考,这是 Instagram DM 页面的屏幕截图。出于隐私原因,我屏蔽了我的用户名和私信我的人的用户名

有人知道如何修改向下滚动页面以适应列大小的脚本吗?

感谢您花时间阅读我的问题并尽您所能提供帮助。

您应该定位要滚动的消息框元素,而不是定位 window:

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

如果它不适用于 class 名称,请尝试使用 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)

根据需要调整滚动条

您应该尝试使用 Instagram 上的导出数据功能,您将获得包含您发送或接收的所有消息和媒体的 JSON 文件。这将使您的任务变得容易得多。

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

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