新买了一台树莓派4B,想运行爬虫脚本。但是安装系统后,安装selenium始终无法成功。树莓派4B是arm架构,现在chrome浏览版本的webdriver不支持。根据网上教程,安装firefox,尝试了多次安装arm下的webdriver仍然没有成功。无奈只能采用pyppeteer,所以记录一下。
以下过程均是在树莓派4B的Raspberry Pi系统下实现的
1.采用pip install pyppeteer
2.(错误尝试,不用执行)终端输入pyppeteer-install安装Chromium。但是发现由于某些原因,无法下载,只能手动下载。Chromium下载地址:CNPM Binaries Mirror (npmmirror.com)。
下载版本需先运行pyppeteer中的chromium_downloader.py文件中的chromium_executable() 函数
运行后我的地址是/home/用户名/.local/share/pyppeteer/local-chromium/588429/chrome-linux/chrome,其中588429及需要的版本
3、下载安装后运行程序,发现仍然无法运行。发现是由于pyppeteer-install下载的Chromium是64位的,但是树莓派4B是32位的,不符合。但是由于Raspberry pi系统中自带Chromium浏览器,我发现只需要在程序中指定Chromium浏览器位置即可,即browser = await launch(executablePath='/usr/bin/chromium-browser'),'/usr/bin/chromium-browser'是在Raspberry pi系统中的Chromium浏览器位置
测试程序如下:
- #! /usr/bin/env python3
- import asyncio
- from pyppeteer import launch
- from pyquery import PyQuery as pq
-
- async def main():
- browser = await launch(executablePath='/usr/bin/chromium-browser')
- page = await browser.newPage()
- await page.goto('http://quotes.toscrape.com/js/')
- await page.screenshot(path='example.png')
- await page.pdf(path='example.pdf')
- dimensions = await page.evaluate('''() => {
- return {
- width: document.documentElement.clientWidth,
- height: document.documentElement.clientHeight,
- deviceScaleFactor: window.devicePixelRatio,
- }
- }''')
-
- print(dimensions)
- # >>> {'width': 800, 'height': 600, 'deviceScaleFactor': 1}
- await browser.close()
-
- asyncio.get_event_loop().run_until_complete(main())