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("http://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("http://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()

異步 REPL,例如 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

你可以使用 Playwright 搭配 Pyinstaller 建立獨立的可執行檔。

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()

如果你想將瀏覽器與可執行檔綑綁在一起:

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 上的 SelectorEventLoop 不相容

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

在 Windows Python 3.7 上,Playwright 將預設事件迴圈設置為 ProactorEventLoop,因為它是 Python 3.8+ 的預設值。

執行緒

Playwright 的 API 不是線程安全的。如果你在多線程環境中使用 Playwright,你應該為每個線程建立一個 playwright 實例。詳情請參閱線程問題