Debain中使用pyppeteer走过的坑
记录自己用Debian
跑 pyppeeter
出的事情
# 开端
主要是是自己要用自动化来处理一下签到的问题,人在学校没法天天签到,就想通过脚本挂着
# 设备
✅自己以前捣鼓的 树莓派4b 2GB
✅自己装了第三方的系统Debian-Pi-Aarch64
(opens new window)
✅finalShell(SSH终端) (opens new window)
# 环境
✅Python 3.7.3
✅Debian GNU/Linux 10 (buster)
# 安装pyppeteer
pip3 install pyppeteer
1
安装完毕后使用pyppeteer-install
安装自带的chromium
# ❎错误:无法启动浏览器
先写个小脚本测试一下
./fly.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
from pyppeteer import launch
async def main():
browser = await launch()
page = await browser.newPage()
await page.goto('https://www.baidu.com')
await page.screenshot({'path': 'example.png'})
await browser.close()
asyncio.run(main())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
运行./fly.py
很牙白❌
错误
Traceback (most recent call last):
File "./fly.py", line 17, in <module>
asyncio.run(main())
File "/usr/lib/python3.7/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
return future.result()
File "./fly.py", line 8, in main
browser = await launch()
File "/usr/local/lib/python3.7/dist-packages/pyppeteer/launcher.py", line 306, in launch
return await Launcher(options, **kwargs).launch()
File "/usr/local/lib/python3.7/dist-packages/pyppeteer/launcher.py", line 148, in launch
self.cmd, **options, )
File "/usr/lib/python3.7/subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.7/subprocess.py", line 1522, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/root/.local/share/pyppeteer/local-chromium/588429/chrome-linux/chrome'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
很明显这个chrome
文件有问题
我尝试使用
chmod 777 chrome
1
但是没用
后来用了Debian
自带的chromium
浏览器
apt-get upgrade
apt install chromium
1
2
2
并且在程序中指向浏览器executablePath='/usr/bin/chromium'
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
from pyppeteer import launch
async def main():
browser = await launch(executablePath='/usr/bin/chromium')
page = await browser.newPage()
await page.goto('https://www.baidu.com')
await page.screenshot({'path': 'example.png'})
await browser.close()
asyncio.run(main())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
可惜运行后还是报错❌
错误
Traceback (most recent call last):
File "./fly.py", line 16, in <module>
asyncio.run(main())
File "/usr/lib/python3.7/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
return future.result()
File "./fly.py", line 8, in main
browser = await launch(executablePath='/usr/bin/chromium')
File "/usr/local/lib/python3.7/dist-packages/pyppeteer/launcher.py", line 306, in launch
return await Launcher(options, **kwargs).launch()
File "/usr/local/lib/python3.7/dist-packages/pyppeteer/launcher.py", line 167, in launch
self.browserWSEndpoint = get_ws_endpoint(self.url)
File "/usr/local/lib/python3.7/dist-packages/pyppeteer/launcher.py", line 226, in get_ws_endpoint
raise BrowserError('Browser closed unexpectedly:\n')
pyppeteer.errors.BrowserError: Browser closed unexpectedly:
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/pyppeteer/launcher.py", line 152, in _close_process
self._loop.run_until_complete(self.killChrome())
File "/usr/lib/python3.7/asyncio/base_events.py", line 560, in run_until_complete
self._check_closed()
File "/usr/lib/python3.7/asyncio/base_events.py", line 480, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
sys:1: RuntimeWarning: coroutine 'Launcher.killChrome' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# ✅解决方式
解决方式
在Linux
使用无头浏览器应当选择非沙盒模式
在代码中加入options={'args': ['--no-sandbox']}
代码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
from pyppeteer import launch
async def main():
browser = await launch(executablePath='/usr/bin/chromium', options={'args': ['--no-sandbox']})
page = await browser.newPage()
await page.goto('https://www.baidu.com')
await page.screenshot({'path': 'example.png'})
await browser.close()
asyncio.run(main())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
✅可以看到没有报错了。
小帮助
如果依然报错,可能是相关库没安装好,运行下列语句安装好就行
apt-get install gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
1
# ❎错误:浏览器乱码
截屏的图居然是乱码?!
提示
Linux
系 (包括Debian,Ubuntu等)系统默认是不安装中文字体的
可以使用命令fc-list :lang=zh
查看
# ✅解决方式
自然只要安装中文字体就好了
以FZMiaoWuJW.TTF
为例
将FZMiaoWuJW.TTF
文件放入/usr/share/fonts
cp FZMiaoWuJW.TTF /usr/share/fonts
1
# 使mkfontscale和mkfontdir命令正常运行
sudo apt-get install ttf-mscorefonts-installer
# 使fc-cache命令正常运行
sudo apt-get install fontconfig
1
2
3
4
2
3
4
给予权限
cd /usr/share/fonts
chmod -R FZMiaoWuJW.TTF 777
1
2
2
mkfontscale
mkfontdir
fc-cache -fv
1
2
3
2
3
可以看到现在有中文字体了
root@flowercloud:/usr/share/fonts/chinese# fc-list :lang=zh
/usr/share/fonts/chinese/FZMiaoWuJW.TTF: FZMiaoWuS\-R\-GB,方正喵呜简体:style=Regular
1
2
2
运行成功
上次更新: 2021/04/10, 23:01:34