Skip to main content

Premièrement, les premiers mots

Adresse du projet:

https://github.com/microsoft/plightTright-python


Deux, préparant

] Avant de vous battre, nous n'avons besoin que de 2 étapes


Étape 1, installez la bibliothèque de dépendance du dramaturge-Python
# 安装依赖库pip3 install playwright

# 安装浏览器驱动python -m playwright install
] ÉTAPE 2, Installez le pilote de navigateur principal
Ceci entraînera le chromédium, Firefox, le navigateur WebKit vers la localité

Lutte






python -m playwright codegen 录制脚本--help 帮助文档-o 生成自动化脚本的目录--target 脚本语言,包含 JS 和 Python,分别对应值为:python 和 javascript-b 指定浏览器驱动比如# 我们通过下面命令打开 Chrome 浏览器开始录制脚本# 指定生成语言为:Python(默认Python,可选)# 保存的文件名:1.py(可选)# 浏览器驱动:webkit(默认webkit,可选)# 最后跟着要打开的目标网站(默认仅仅是打开浏览器,可选)python -m playwright codegen --target python -o '1.py' -b webkit https://www.baidu.com


from playwright import sync_playwrightdef run(playwright): browser = playwright.webkit.launch(headless=False) context = browser.newContext() # Open new page page = context.newPage() # Go to https://www.baidu.com/ page.goto("https://www.baidu.com/") # Fill input[name="wd"] page.fill("input[name='wd']", "AirPython") # Press Enter # with page.expect_navigation(url="https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=AirPython&fenlei=256&rsv_pq=a1739d870005eec3&rsv_t=e640wwS33ra1Koivxvy1WyTxyknRwnllWiw4JBqIYd/KUN/WKpWLtL2b2+0&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=21&rsv_sug1=18&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=6199&rsv_sug4=6199"): with page.expect_navigation(): page.press("input[name='wd']", "Enter") # Close page page.close() # --------------------- context.close() browser.close()with sync_playwright() as playwright: run(playwright)

from time import sleepfrom playwright import sync_playwright# 注意:默认是无头模式with sync_playwright() as p: # 分别对应三个浏览器驱动 for browser_type in [p.chromium, p.firefox, p.webkit]: # 指定为有头模式,方便查看 browser = browser_type.launch(headless=False) page = browser.newPage() page.goto('http://baidu.com') # 执行一次搜索操作 page.fill("input[name='wd']", "AirPython") with page.expect_navigation(): page.press("input[name='wd']", "Enter") # 等待页面加载完全 page.waitForSelector("text=百度热榜") # 截图 page.screenshot(path=f'example-{browser_type.name}.png') # 休眠5s sleep(5) # 关闭浏览器 browser.close()


import asynciofrom playwright import async_playwright# 异步执行async def main(): async with async_playwright() as p: for browser_type in [p.chromium, p.firefox, p.webkit]: # 指定为有头模式,方便查看 browser = await browser_type.launch(headless=False) page = await browser.newPage() await page.goto('http://baidu.com') # 执行一次搜索操作 await page.fill("input[name='wd']", "AirPython") await page.press("input[name='wd']", "Enter") # 等待页面加载完全 await page.waitForSelector("text=百度热榜") # 截图 await page.screenshot(path=f'example-{browser_type.name}.png') await browser.close()asyncio.get_event_loop().run_until_complete(main()) . Simulation de recherche de navigateur une fois, puis fermez le navigateur , le script automatique créera automatiquement, sauvegardez-le dans le fichier 2, Sync Le mot clé est synchronisé comme suit: synchronisée Sync_playwright Exemple: Nous utilisons trois tours le noyau de navigateur ouvrira le navigateur, puis Baidu, puis la capture d'écran de l'interfaceRecherche, enfin fermer le navigateur , l'API intègre le dramaturge-Python inclut fondamentalement les opérations d'automatisation générales 3, asynchrone async_playwright combinée avec Asyncio, nous avons effectué en même temps Enfin En réalité, le dramaturge en tant que cadrage de l'automatisation inverse, supporte Python, Java, JS, etc. dramaturge Par rapport au cadre automatique traditionnel de sélénium, il est concis et plus fort dans le contexte et le contexte de l'API, plus détaillé. La fonction peut être déverrouillée en lisant le texte d'origine.

Sujets

Catégories