最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Mac中初始化搭建Python版puppeteer的pyppeteer的开发环境

Mac crifan 1498浏览 0评论
折腾:
【未解决】Mac中用puppeteer自动操作浏览器实现百度搜索
期间,先参考自己教程
puppeteer · 解放你的双手:自动化测试
puppeteer python
对应python版本是:pyppeteer
pyppeteer
pyppeteer · PyPI
pip install pyppeteer
pyppeteer/pyppeteer: Headless chrome/chromium automation library (unofficial port of puppeteer)
但是是非官方
最后更新:2021  9 Jan
miyakogi/pyppeteer: Headless chrome/chromium automation library (unofficial port of puppeteer)
已经archive了
最后更新:8 May 2020
API Reference — Pyppeteer 0.0.25 documentation
Pyppeteer’s documentation — Pyppeteer 0.0.25 documentation
pyppeteer.page — Pyppeteer 0.0.25 documentation
 crifan@licrifandeMacBook-Pro  ~/dev/dev_root/python/puppeteerBaiduSearch  pip install pyppeteer
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting pyppeteer
  Downloading http://mirrors.aliyun.com/pypi/packages/4a/05/ea3250282e46fda60df1f1d5246bb8cdc022abb89969c61a98ea28fd6b82/pyppeteer-0.2.5-py3-none-any.whl (87 kB)
     |████████████████████████████████| 87 kB 1.6 MB/s 
Collecting websockets<9.0,>=8.1
  Downloading http://mirrors.aliyun.com/pypi/packages/1b/d0/df088c0bcb09efdf643f26df5ba3e40bb7e79cae34b3fa48d547da658c5c/websockets-8.1-cp36-cp36m-macosx_10_6_intel.whl (66 kB)
     |████████████████████████████████| 66 kB 6.5 MB/s 
Collecting importlib-metadata<3.0.0,>=2.1.1
  Downloading http://mirrors.aliyun.com/pypi/packages/98/b8/8ec57a8ef46fbe7f185318c7ff7df9a06c9df451d9a59a067bfa851bb828/importlib_metadata-2.1.1-py2.py3-none-any.whl (10 kB)
Collecting tqdm<5.0.0,>=4.42.1
  Downloading http://mirrors.aliyun.com/pypi/packages/f8/3e/2730d0effc282960dbff3cf91599ad0d8f3faedc8e75720fdf224b31ab24/tqdm-4.59.0-py2.py3-none-any.whl (74 kB)
     |████████████████████████████████| 74 kB 1.3 MB/s 
Collecting pyee<9.0.0,>=8.1.0
  Downloading http://mirrors.aliyun.com/pypi/packages/0d/0a/933b3931107e1da186963fd9bb9bceb9a613cff034cb0fb3b0c61003f357/pyee-8.1.0-py2.py3-none-any.whl (12 kB)
Requirement already satisfied: urllib3<2.0.0,>=1.25.8 in /Users/crifan/.pyenv/versions/3.6.6/lib/python3.6/site-packages (from pyppeteer) (1.25.8)
Collecting appdirs<2.0.0,>=1.4.3
  Downloading http://mirrors.aliyun.com/pypi/packages/3b/00/2344469e2084fb287c2e0b57b72910309874c3245463acd6cf5e3db69324/appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Collecting zipp>=0.5
  Downloading http://mirrors.aliyun.com/pypi/packages/0f/8c/715c54e9e34c0c4820f616a913a7de3337d0cd79074dd1bed4dd840f16ae/zipp-3.4.1-py3-none-any.whl (5.2 kB)
Installing collected packages: zipp, websockets, tqdm, pyee, importlib-metadata, appdirs, pyppeteer
Successfully installed appdirs-1.4.4 importlib-metadata-2.1.1 pyee-8.1.0 pyppeteer-0.2.5 tqdm-4.59.0 websockets-8.1 zipp-3.4.1
安装后,去写代码试试
https://miyakogi.github.io/pyppeteer/index.html
首次使用时,会下载Chromium,约100MB
如果不想内部自动安装,则手动事先安装:pyppeteer-install
 which pyppeteer-install      
/Users/crifan/.pyenv/shims/pyppeteer-install
发现是有的。
那去安装试试:
 pyppeteer-install
[W:pyppeteer.chromium_downloader] start chromium download.
Download may take a few minutes.
  2%|█▎                                                                       | 1607680/86759503 [00:09<05:22, 263751.77it/s]
API Reference — Pyppeteer 0.0.25 documentation
安装完毕:
 pyppeteer-install
[W:pyppeteer.chromium_downloader] start chromium download.
Download may take a few minutes.
100%|████████████████████████████████████████████████████████████████████████| 86759503/86759503 [02:38<00:00, 546119.68it/s]
[W:pyppeteer.chromium_downloader] 
chromium download done.
[W:pyppeteer.chromium_downloader] chromium extracted to: /Users/crifan/Library/Application Support/pyppeteer/local-chromium/588429
继续去写代码
import asyncio
from pyppeteer import launch


async def main():
    browser = await launch()
    page = await browser.newPage()
    baiduUrl = "https://www.baidu.com"
    await page.goto(baiduUrl)
    await page.screenshot({'path': 'baidu.png'})
    await browser.close()


asyncio.get_event_loop().run_until_complete(main())
调试发现:
没有启动Chrome浏览器啊
但是可以继续调试
不过后续功能是正常的:
可以截图出来
至此,算是环境基本OK了。
但是要去搞清楚:
【已解决】pyppeteer功能正常但是没有启动Chrome浏览器
至此,环境才算正常。
 【总结】
Mac中搭建pyppeteer的环境:
(1)安装pyppeteer
pip install pyppeteer
(2)安装(脚本运行时所需的)Chromium浏览器(约100MB)
pyppeteer-install
此处安装到了:
/Users/crifan/Library/Application Support/pyppeteer/local-chromium/588429
(3)写代码
import asyncio
from pyppeteer import launch


async def main():
    browser = await launch(headless=False)
    page = await browser.newPage()
    baiduUrl = "https://www.baidu.com"
    await page.goto(baiduUrl)
    await page.screenshot({'path': 'baidu.png'})
    await browser.close()


asyncio.get_event_loop().run_until_complete(main())
即可:
启动Chromium浏览器,并打开百度,和本地截图
效果:

转载请注明:在路上 » 【已解决】Mac中初始化搭建Python版puppeteer的pyppeteer的开发环境

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
96 queries in 0.216 seconds, using 20.09MB memory