Playwright环境搭建
版本要求
python3.7+ 版本,推荐python3.8
环境准备
安装 playwright
pip install playwright
安装所需的浏览器,chromium,firefox和webkit
playwright install
快速开始
from playwright.sync_api import sync_playwright with sync_playwright() as p: browser = p.chromium.launch(headless=False) #谷歌浏览器有头模板打开浏览器,若未写headless=False,则是无头模式运行 page = browser.new_page() #打开一个page页 page.goto("https://www.baidu.com") #打开百度 page.wait_for_timeout(5000) # 等待5秒 page.locator('#kw').fill("playwright") #输入playwright page.locator("#su").click() #点击搜索 browser.close() #关闭浏览器对象