最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Mac中安装Python版Playwright和初始化开发环境

Python crifan 762浏览 0评论
折腾:
【未解决】Mac中用playwright自动操作浏览器实现百度搜索
期间,参考Python版Playwright的官网教程:
microsoft/playwright-python: Python version of the Playwright testing and automation library. (github.com)
去Mac中搭建开发环境和初始化。
 [email protected]  ~/dev/crifan/python/BaiduSearchAutomation/playwright  pip install playwright
Collecting playwright
  Downloading playwright-1.10.0-py3-none-macosx_10_13_x86_64.whl (17.8 MB)
     |████████████████████████████████| 17.8 MB 2.4 MB/s 
Collecting greenlet==1.0.0
  Downloading greenlet-1.0.0-cp38-cp38-macosx_10_14_x86_64.whl (86 kB)
     |████████████████████████████████| 86 kB 2.5 MB/s 
Collecting typing-extensions; python_version <= "3.8"
  Downloading typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Collecting pyee>=8.0.1
  Downloading pyee-8.1.0-py2.py3-none-any.whl (12 kB)
Installing collected packages: greenlet, typing-extensions, pyee, playwright
Successfully installed greenlet-1.0.0 playwright-1.10.0 pyee-8.1.0 typing-extensions-3.7.4.3
去看看:
 which playwright
/Users/xxx/.pyenv/shims/playwright
和:
 playwright --help        
Usage: npx playwright [options] [command]


Options:
  -V, --version                          output the version number
  -h, --help                             display help for command


Commands:
  open [options] [url]                   open page in browser specified via -b, --browser
  codegen [options] [url]                open page and generate code for user actions
  debug <app> [args...]                  run command in debug mode: disable timeout, open inspector
  install [browserType...]               ensure browsers necessary for this version of Playwright are installed
  install-deps [browserType...]          install dependencies necessary to run browsers (will ask for sudo permissions)
  cr [options] [url]                     open page in Chromium
  ff [options] [url]                     open page in Firefox
  wk [options] [url]                     open page in WebKit
  screenshot [options] <url> <filename>  capture a page screenshot
  pdf [options] <url> <filename>         save page as pdf
  help [command]                         display help for command
接着去初始化安装:
 playwright install
Downloading chromium v857950 - 113.9 Mb [                    ] 1% 384.5s 
等待
官网
Fast and reliable end-to-end testing for modern web apps | Playwright
Getting Started | Playwright
Getting Started | Playwright
Playwright | Playwright
Community Showcase | Playwright
安装完毕:
 playwright install
Downloading chromium v857950 - 113.9 Mb [====================] 100% 0.0s 
chromium v857950 downloaded to /Users/xxx/Library/Caches/ms-playwright/chromium-857950
Downloading firefox v1238 - 75 Mb [====================] 100% 0.0s 
firefox v1238 downloaded to /Users/xxx/Library/Caches/ms-playwright/firefox-1238
Downloading webkit v1443 - 52 Mb [====================] 100% 0.0s 
webkit v1443 downloaded to /Users/xxx/Library/Caches/ms-playwright/webkit-mac10.14-special-1443
Downloading ffmpeg v1005 - 1.3 Mb [====================] 100% 0.0s 
ffmpeg v1005 downloaded to /Users/xxx/Library/Caches/ms-playwright/ffmpeg-1005
去写代码:
# Function: Playwright demo baidu search
# Author: Crifan Li
# Update: 20210331


from playwright.sync_api import sync_playwright


with sync_playwright() as p:
    chromiumBrowser = p.chromium
    browser = chromiumBrowser.launch()
    page = browser.new_page()
    page.goto('http://www.baidu.com')
    page.screenshot(path=f'example_chromium.png')
    browser.close()
调试看看
是有:
<BrowserType name=chromium executable_path=/Users/xxx/Library/Caches/ms-playwright/chromium-857950/chrome-mac/Chromium.app/Contents/MacOS/Chromium>
继续调试。
代码:
from playwright.sync_api import sync_playwright


with sync_playwright() as p:
    chromiumBrowserType = p.chromium
    print("chromiumBrowserType=%s" % chromiumBrowserType)
    browser = chromiumBrowserType.launch()
    # chromiumBrowserType=<BrowserType name=chromium executable_path=/Users/xxx/Library/Caches/ms-playwright/chromium-857950/chrome-mac/Chromium.app/Contents/MacOS/Chromium>
    print("browser=%s" % browser)
    # browser=<Browser type=<BrowserType name=chromium executable_path=/Users/xxx/Library/Caches/ms-playwright/chromium-857950/chrome-mac/Chromium.app/Contents/MacOS/Chromium> version=90.0.4430.0>
    page = browser.new_page()
    print("page=%s" % page)
    # page=<Page url='about:blank'>
    page.goto('http://www.baidu.com')
    print("page=%s" % page)
    # page=<Page url='https://www.baidu.com/'>
    page.screenshot(path=f'example_chromium.png')
    browser.close()
输出:
chromiumBrowserType=<BrowserType name=chromium executable_path=/Users/xxx/Library/Caches/ms-playwright/chromium-857950/chrome-mac/Chromium.app/Contents/MacOS/Chromium>
browser=<Browser type=<BrowserType name=chromium executable_path=/Users/xxx/Library/Caches/ms-playwright/chromium-857950/chrome-mac/Chromium.app/Contents/MacOS/Chromium> version=90.0.4430.0>
page=<Page url='about:blank'>
page=<Page url='https://www.baidu.com/'>
说明正常能跑了。初始化结束了。
不过要搞清楚,如何显示浏览器,即非无头模式:
看到官网
microsoft/playwright-python: Python version of the Playwright testing and automation library. (github.com)
“# Use playwright.chromium, playwright.firefox or playwright.webkit
# Pass headless=False to see the browser UI”
去试试:
browser = chromiumBrowserType.launch(headless=False)
结果:
是可以启动Chromium浏览器的。
继续,是可以看到打开了百度首页的:
【总结】
此处Mac中初始化Python版Playwright环境:
安装playwright:
pip install playwright
初始化playwright,去安装浏览器内核:
playwright install
此处下载安装了:
  • chromium
    • 位置:/Users/xxx/Library/Caches/ms-playwright/chromium-857950
    • 效果:
  • firefox
    • 位置:/Users/xxx/Library/Caches/ms-playwright/firefox-1238
    • 效果:
  • webkit
    • 位置:/Users/xxx/Library/Caches/ms-playwright/webkit-mac10.14-special-1443
    • 效果:
  • ffmpeg
    • 位置:/Users/xxx/Library/Caches/ms-playwright/ffmpeg-1005
    • 效果:
演示代码:
# Function: Playwright demo baidu search
# Author: Crifan Li
# Update: 20210331


from playwright.sync_api import sync_playwright


with sync_playwright() as p:
    chromiumBrowserType = p.chromium
    print("chromiumBrowserType=%s" % chromiumBrowserType)
    browser = chromiumBrowserType.launch(headless=False)
    # chromiumBrowserType=<BrowserType name=chromium executable_path=/Users/xxx/Library/Caches/ms-playwright/chromium-857950/chrome-mac/Chromium.app/Contents/MacOS/Chromium>
    print("browser=%s" % browser)
    # browser=<Browser type=<BrowserType name=chromium executable_path=/Users/xxx/Library/Caches/ms-playwright/chromium-857950/chrome-mac/Chromium.app/Contents/MacOS/Chromium> version=90.0.4430.0>
    page = browser.new_page()
    print("page=%s" % page)
    # page=<Page url='about:blank'>
    page.goto('http://www.baidu.com')
    print("page=%s" % page)
    # page=<Page url='https://www.baidu.com/'>
    page.screenshot(path=f'example_chromium.png')
    browser.close()
输出:
chromiumBrowserType=<BrowserType name=chromium executable_path=/Users/xxx/Library/Caches/ms-playwright/chromium-857950/chrome-mac/Chromium.app/Contents/MacOS/Chromium>
browser=<Browser type=<BrowserType name=chromium executable_path=/Users/xxx/Library/Caches/ms-playwright/chromium-857950/chrome-mac/Chromium.app/Contents/MacOS/Chromium> version=90.0.4430.0>
page=<Page url='about:blank'>
page=<Page url='https://www.baidu.com/'>
效果:

转载请注明:在路上 » 【已解决】Mac中安装Python版Playwright和初始化开发环境

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.128 seconds, using 20.71MB memory