python安装playwright
python 安装playwright
playwright 安装:
同人小说是什么
安装playwright及Chromium、Firefox和WebKit的浏览器⼆进制⽂件
pip安装
conda安装
安装成功后,导⼊模块,简单(同步)运⾏⼀个案例:
也可以⽤异步的形式:
执⾏前可以在初始化浏览器对象时根据需要加参数
佛说爱情使⽤playwright录制脚本,根据浏览器操作,⾃动⽣成代码
终端执⾏:pip install --upgrade pip pip install playwright playwright install
1
2
3conda config --add channels conda -forge conda config --add channels microsoft conda install playwright playwright install
1
梅干菜扣肉的做法2
3
去埃及旅游
4from  playwright .sync_api import  sync_playwright with  sync_playwright () as  sp :    browser = sp .chromium .launch ()    page = browser .new_page ()    page .goto ("playwright.dev")    print (page .title ())    browser .close ()
1
2
3
4溪水
5
6
7
8import  asyncio from  playwright .async_api import  async_playwright async  def  main ():    async  with  async_playwright () as  ap :        browser = await  ap .chromium .launch ()        page = await  browser .new_page ()        await  page .goto ("playwright.dev")        print (await  page .title ())        await  browser .close ()
1
2
3
4
5
6
7
8
9
10# playwright 默认是⽆头模式,可以设置headless 参数打开浏览器UI 界⾯,slow_mo 设置请求速度sp .chromium .launch (headless =False , slow_mo =50)
1
2# (异步)等待页⾯加载操作,可以⽤来替代time.sleep()操作:page .wait_for_timeout (5000)
1
2playwright codegen https ://www .bilibili
1
右边页⾯为Inspector,时Playwright 的⼀个GUI ⼯具,可帮助创作和调试 Playwright 脚本
python交互模式与IDE中类似
需要注意的是,由于不是⽤playwright上下⽂创建的,需要在最后⼿动关闭
同步模式:异步模式
python -m asyncio 启动程序
女生打扮
另外还可以使⽤playwright和pyinstaller结合⽣成执⾏⽂件>>> 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 ("/")>>> page .screenshot (path="example.png")>>> browser .close ()>>>> playwright .stop ()
1
2
3
4
5
6
7
8
910
11>>> 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 ("/")>>> await page .screenshot (path="example.png")>>> await browser .close ()>>>> await playwright .stop ()
1
2
3
4
5
6
7
8
9PLAYWRIGHT_BROWSERS_PATH=0 playwright install chromium pyinstaller -F main .py
1
2
没有pyinstaller 模块的话可以⼿动安装
官⽹原话:
incompatible with SelectorEventLoop of asyncio on Windows  Playwright runs the driver in a subprocess, so it requires ProactorEventLoop of asyncio on Windows because SelectorEventLoop does not supports async subprocesses. 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. 在windows上使⽤asyncio异步执⾏程序时,需要⽤ProactorEventLoop替代SelectorEventLoop,因为SelectorEventLoop 不⽀持异步⼦进程。需要注意的是,在多线程中使⽤playwright时,需要给每个线程初始化⼀个playwright对象pip install pyinstaller
1

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。