【问题标题】:How to use the Playwright library in jupyter notebook instead of using a .py script如何在 jupyter notebook 中使用 Playwright 库而不是使用 .py 脚本
【发布时间】:2022-12-21 22:39:06
【问题描述】:

我想使用自动浏览器并使用 jupyter notebook 单元执行我的步骤,而不是使用 .py 脚本。这适用于名为 selenium 的浏览器自动化库。

它不适用于名为 Playwright 的库。事实上它根本不起作用。我尝试了他们手册中提供的每一行代码。在 jupyter notebooks 中没有任何作用。只要在某个 .py 文件中复制粘贴相同的代码并执行它,在我的机器上一切正常。 我正在谈论的各种例子可以在这里找到:https://playwright.dev/python/docs/intro

我真的不明白为什么我不能让它在 jupyter notebook 中工作,特别是如果它在每个 .py 文件中都能正常工作。

【问题讨论】:

  • 对这些问题表示同情,但这种描述会受益于一些关于什么不起作用的具体细节。 “NOTHING”没有那么有用,它不表示任何错误消息的性质,它们发生的时间或其他用于诊断情况的合理细节
  • 不,它不会。我是认真的,就像我说的那样。字面上地。您从他们的示例中获取任何代码示例,然后将其放入 .py 脚本中,它会按预期工作。使用 jupyter notebook,但没有任何效果。没有什么。不难理解不是吗?
  • 我的观点不是关于易于理解,而是从其他人能够帮助你的角度来看,这个问题写得不是特别好。
  • 感谢您的反馈意见。这里是我想要的解决方案:从我提供的链接中获取任何代码示例,使其在 jupyter 笔记本(使用 Windows)中工作并解决问题

标签: python python-3.x webdriver playwright playwright-python


【解决方案1】:

https://github.com/microsoft/playwright-python/issues/480所述

Jupyter notebook 使用 asyncio 事件循环,所以你应该使用 async api。

from playwright.async_api import async_playwright

playwright = await async_playwright().start()
browser = await playwright.chromium.launch(headless = False)
page = await browser.new_page()

await page.goto("http://whatsmyuseragent.org/")

# await page.screenshot(path="example.png")
# await browser.close()
# await playwright.stop()


如果你使用sync API,它会抛出这样的错误:

from playwright.sync_api import sync_playwright
playwright = sync_playwright().start()

'''
Error: It looks like you are using Playwright Sync API inside the asyncio loop.
Please use the Async API instead.
'''

【讨论】:

  • 这意味着有一个修复,但尽管各种 GitHub 问题已正式关闭,据我所知这实际上仍然是不可能的。在此处查看 cmets:github.com/microsoft/playwright-python/issues/…
  • 事实上,它适用于我的 jupyter notebook。我可以在其中测试代码。
  • 感谢 Ferris 的更新。看起来你在 Mac 上,这可能解释了它,但在 Windows 上(和根据评论的 WSL)我知道它仍然无法正常工作。我现在挂机了,但我会尽快再次检查您的解决方案
  • 我使用 Windows,此代码会为我抛出一个错误。但如果它至少可以在 mac 上运行,那就太好了
【解决方案2】:

如果你不能在jupyter notebook中使用async API,你可以尝试为playwright创建一个虚拟环境:

在终端:

# create a virtual environment for playwright
python3 -m venv playwright_new
source ~/playwright_new/bin/activate
pip install playwright ipykernel requests 
playwright install

然后,为 jupyter notebook 创建一个内核链接:

source ~/playwright_new/bin/activate

# create kernel link for jupyter notebook 
python -m ipykernel install --user --name playwright_new --display-name "playwright_new"

# in mac
ls /Users/xxx/Library/Jupyter/kernels/
tree /Users/xxx/Library/Jupyter/kernels/playwright_new
    /Users/xxx/Library/Jupyter/kernels/playwright_new
    ├── kernel.json
    ├── logo-32x32.png
    └── logo-64x64.png

# or in linux
tree /root/.local/share/jupyter/kernels

然后,再次运行 python 代码。

from playwright.async_api import async_playwright

playwright = await async_playwright().start()
browser = await playwright.chromium.launch(headless = False)
page = await browser.new_page()

await page.goto("http://whatsmyuseragent.org/")

错误调试


if exec python code throws an Error:

Error: Executable doesn't exist at /Users/xxxx/Library/Caches/ms-playwright/chromium-1000/chrome-mac/Chromium.app/Contents/MacOS/Chromium
╔═════════════════════════════════════════════════════════════════════════╗
║ Looks like Playwright Test or Playwright was just installed or updated. ║
║ Please run the following command to download new browsers:              ║
║                                                                         ║
║     playwright install                                                  ║
║                                                                         ║
║ <3 Playwright Team                                                      ║
╚═════════════════════════════════════════════════════════════════════════╝

在终端:

# you already install playwright
playwright install 

cd /Users/xxxx/Library/Caches/ms-playwright
ls
    chromium-978106/ ffmpeg-1007/     firefox-1319/    webkit-1616/

# but the folder ms-playwright/chromium-1000 NOT EXISTS
# COPY the exists chromium folder with a new name  `chromium-1000`
cp -r chromium-978106 chromium-1000

【讨论】:

    【解决方案3】:

    由于 Colab 笔记本是托管的 Jupyter 笔记本,我建议使用以下解决方案在托管的 Jupyter 实例中运行 playwright。

    我只在我的 Google Colab notebook 中测试过,并没有在本地托管的 Jupyter 实例中测试过。

    Playwright in Google Colab Solution

    【讨论】:

      猜你喜欢
      • 2019-07-20
      • 2017-04-21
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 2023-01-21
      • 1970-01-01
      相关资源
      最近更新 更多