pyppeteer安装chromium太慢

在安装chromium的时候,因为是谷歌源所以我们下载不了或者下载很慢,并且网上大多数的资料都是复制别人的,并没有给出具体的解决办法,很坑,这里给出我搞了一天的解决办法,有点复杂

我们选择系统默认的版本,这样最保险

用清华源安装pyppeteer

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pyppeteer

然后我们在新建一个py文件写入一下代码

from pyppeteer import chromium_downloader

print(chromium_downloader.DEFAULT_DOWNLOAD_HOST)
print(chromium_downloader.downloadURLs)
print(chromium_downloader.BASE_URL)

"""
https://storage.googleapis.com
{'linux': 'https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/575458/chrome-linux.zip', 'mac': 'https://storage.googleapis.com/chromium-browser-snapshots/Mac/575458/chrome-mac.zip', 'win32': 'https://storage.googleapis.com/chromium-browser-snapshots/Win/575458/chrome-win32.zip', 'win64': 'https://storage.googleapis.com/chromium-browser-snapshots/Win_x64/575458/chrome-win32.zip'}
https://storage.googleapis.com/chromium-browser-snapshots
"""

可以发现,这里的chromium下载源是国外的,国外的是普遍的慢
其实chromium源也有类似于pip一样的国内镜像源
例如 https://npm.taobao.org/mirrors/chromium-browser-snapshots/

  1. 我们可以查看下其pyppeteer源码,位置在$python\Lib\site-packages\pyppeteer\chromium_downloader.py
  2. 该源码 中的DEFAULT_DOWNLOAD_HOST字段,字段的值为https://storage.googleapis.com
  3. 我们改变它的值,设置为 DEFAULT_DOWNLOAD_HOST = 'https://npm.taobao.org/mirrors'

这样,我们在一次打印chromium下载器的信息就会发生改变

https://npm.taobao.org/mirrors
{'linux': 'https://npm.taobao.org/mirrors/chromium-browser-snapshots/Linux_x64/575458/chrome-linux.zip', 'mac': 'https://npm.taobao.org/mirrors/chromium-browser-snapshots/Mac/575458/chrome-mac.zip', 'win32': 'https://npm.taobao.org/mirrors/chromium-browser-snapshots/Win/575458/chrome-win32.zip', 'win64': 'https://npm.taobao.org/mirrors/chromium-browser-snapshots/Win_x64/575458/chrome-win32.zip'}
https://npm.taobao.org/mirrors/chromium-browser-snapshots

接下来通过pyppeteer安装chromium的速度就会倍速的增大

from pyppeteer import chromium_downloader

chromium_downloader.download_chromium()
print(chromium_downloader.check_chromium())
[W:pyppeteer.chromium_downloader] start chromium download.
Download may take a few minutes.
https://npm.taobao.org/mirrors/chromium-browser-snapshots
100%|██████████| 133194757/133194757 [00:22<00:00, 6053700.03it/s]
[W:pyppeteer.chromium_downloader] 
chromium download done.
[W:pyppeteer.chromium_downloader] chromium extracted to: C:\Users\Administrator\AppData\Local\pyppeteer\pyppeteer\local-chromium\575458

你可能感兴趣的:(Pyppeteer)