- 安装
pip uninstall playwright -y
pip install playwright --upgrade
- 安装浏览器
# 安装浏览器,不指定名称,会自动安装firfox,edge,chromium
playwright install chromium
复制URL去浏览器下载,速度快20倍
https://playwright.azureedge.net/builds/chromium/1129/chromium-win64.zip
- 同步
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()
- 异步
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())