Skip to main content

入門 - 程式庫

安裝

Pip

PyPI version

pip install --upgrade pip
pip install playwright
playwright install

Conda

Anaconda version

conda config --add channels conda-forge
conda config --add channels microsoft
conda install playwright
playwright install

這些命令會下載 Playwright 套件並為 Chromium、Firefox 和 WebKit 安裝瀏覽器二進位檔案。要修改此行為,請參閱安裝參數

使用方式

安裝後,您可以在 Python 腳本中 import Playwright,並啟動 3 種瀏覽器中的任何一種(chromiumfirefoxwebkit)。

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://playwright.dev")
print(page.title())
browser.close()

Playwright 支援兩種 API 變體:同步和非同步。如果您的現代專案使用 asyncio,您應該使用非同步 API:

import asyncio
from playwright.async_api import async_playwright

async def main():
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
await page.goto("https://playwright.dev")
print(await page.title())
await browser.close()

asyncio.run(main())

第一個腳本

在我們的第一個腳本中,我們將導航到 https://playwright.dev/ 並在 WebKit 中截取螢幕截圖。

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
browser = p.webkit.launch()
page = browser.new_page()
page.goto("https://playwright.dev/")
page.screenshot(path="example.png")
browser.close()

預設情況下,Playwright 在無頭模式下執行瀏覽器。要查看瀏覽器 UI,請將 headless 選項設定為 False。您也可以使用 slow_mo 來減慢執行速度。在偵錯工具章節中了解更多。

firefox.launch(headless=False, slow_mo=50)

互動模式(REPL)

您可以啟動互動式 Python REPL:

python

然後在其中啟動 Playwright 進行快速實驗:

from playwright.sync_api import sync_playwright
playwright = sync_playwright().start()
# Use playwright.chromium, playwright.firefox or playwright.webkit
# Pass headless=False to launch() to see the browser UI
browser = playwright.chromium.launch()
page = browser.new_page()
page.goto("https://playwright.dev/")
page.screenshot(path="example.png")
browser.close()
playwright.stop()

Async REPL such as asyncio REPL:

python -m asyncio
from playwright.async_api import async_playwright
playwright = await async_playwright().start()
browser = await playwright.chromium.launch()
page = await browser.new_page()
await page.goto("https://playwright.dev/")
await page.screenshot(path="example.png")
await browser.close()
await playwright.stop()

Pyinstaller

You can use Playwright with Pyinstaller to create standalone executables.

main.py
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://playwright.dev/")
page.screenshot(path="example.png")
browser.close()

If you want to bundle browsers with the executables:

PLAYWRIGHT_BROWSERS_PATH=0 playwright install chromium
pyinstaller -F main.py
note

將瀏覽器與可執行檔一起打包會產生更大的二進位檔案。建議只打包您使用的瀏覽器。

已知問題

time.sleep() 導致過時狀態

您很可能不需要手動等待,因為 Playwright 具有自動等待功能。如果您仍然依賴它,您應該使用 page.wait_for_timeout(5000) 而不是 time.sleep(5),最好根本不等待逾時,但有時對於偵錯很有用。在這些情況下,請使用我們的等待(wait_for_timeout)方法而不是 time 模組。這是因為我們內部依賴非同步操作,當使用 time.sleep(5) 時,它們無法正確處理。

與 Windows 上 asyncioSelectorEventLoop 不相容

Playwright 在子程序中執行驅動程式,因此在 Windows 上需要 asyncioProactorEventLoop,因為 SelectorEventLoop 不支援非同步子程序。

On Windows Python 3.7, Playwright sets the default event loop to ProactorEventLoop as it is default on Python 3.8+.

Threading

Playwright's API is not thread-safe. If you are using Playwright in a multi-threaded environment, you should create a playwright instance per thread. See threading issue for more details.