You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python Playwright实现Instagram粉丝弹窗滚动抓取

解决Playwright滚动Instagram粉丝弹窗的问题

你遇到的核心问题是:page.mouse.wheel()作用于整个页面,而粉丝弹窗是独立的滚动容器,需要定位到这个容器再执行滚动操作。以下是针对Playwright的解决方案:

核心思路

Instagram的粉丝弹窗是独立模态框,内部包含可滚动的用户列表容器。我们需要先定位这个容器,再对其执行滚动操作,而非直接操作整个页面。

具体实现方法

方法1:使用Playwright原生scroll()方法(推荐)

定位到粉丝弹窗的滚动容器后,直接调用scroll()方法滚动:

# 打开粉丝弹窗后,定位滚动容器
followers_container = page.locator("div[role='dialog'] div.x9f619.x1n2onr6.x1ja2u2z.x78zum5.xdt5ytf.x1iyjqo2")

# 循环滚动加载更多粉丝(可根据需求调整次数或终止条件)
for _ in range(5):
    followers_container.scroll(direction="down", distance=2000)
    page.wait_for_timeout(1000)  # 等待加载(替代time.sleep,更符合Playwright规范)

方法2:对容器执行wheel操作

如果scroll()方法效果不佳,可直接对容器模拟鼠标滚轮:

followers_container = page.locator("div[role='dialog'] div.x9f619.x1n2onr6.x1ja2u2z.x78zum5.xdt5ytf.x1iyjqo2")

for _ in range(5):
    followers_container.wheel(0, 2000)
    page.wait_for_timeout(1000)

方法3:通过JavaScript强制滚动

若前两种方法失效,可直接用JS操作容器的滚动属性:

followers_container = page.locator("div[role='dialog'] div.x9f619.x1n2onr6.x1ja2u2z.x78zum5.xdt5ytf.x1iyjqo2")

for _ in range(5):
    followers_container.evaluate("el => el.scrollTop = el.scrollHeight")
    page.wait_for_timeout(1500)

修改后的完整代码

将你原代码中的滚动部分替换为上述代码,示例如下:

from playwright.sync_api import Playwright, sync_playwright, expect 


def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()

    # Open new page
    page = context.new_page()

    # Go to https://www.instagram.com/
    page.goto("https://www.instagram.com/")

    # Click on Username field
    page.locator("[aria-label=\"Phone number\\, username\\, or email\"]").click()

    # Fill with username
    page.locator("[aria-label=\"Phone number\\, username\\, or email\"]").fill("USERNAME")

    # Click on Password field
    page.locator("[aria-label=\"Password\"]").click()

    # Fill with password
    page.locator("[aria-label=\"Password\"]").fill("PASSWORD")

    # Click Log In
    page.locator("button:has-text(\"Log In\")").first.click()
    page.wait_for_url("https://www.instagram.com/accounts/onetap/? next=%2F")

    # Click text=Not Now
    page.locator("text=Not Now").click()
    page.wait_for_url("https://www.instagram.com/")

    # Click text=Not Now
    page.locator("text=Not Now").click()

    page.goto("https://www.instagram.com/instagram/")

    # Click text=542M followers
    page.locator("text=542M followers").click()
    page.wait_for_url("https://www.instagram.com/instagram/followers/")

    # 替换原滚动代码,使用Playwright定位容器滚动
    followers_container = page.locator("div[role='dialog'] div.x9f619.x1n2onr6.x1ja2u2z.x78zum5.xdt5ytf.x1iyjqo2")
    # 循环滚动5次,可根据需要调整
    for _ in range(5):
        followers_container.scroll(direction="down", distance=2000)
        page.wait_for_timeout(1000)

    # 关闭浏览器
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)

注意事项

  • Instagram的元素类名可能随版本更新变化,如果定位失败,可重新用Playwright Codegen抓取滚动容器的选择器。
  • 避免过度频繁滚动,防止触发Instagram的反爬机制。
  • 优先使用page.wait_for_timeout()替代time.sleep(),更适配Playwright的执行逻辑。

内容的提问来源于stack exchange,提问作者Mhmd Khalil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 20:33:21