pyppeteer-在代理后面安装

pyppeteer-install behind proxy

我在公司代理后面。

我可以通过 set https_proxy=http://myproxy:port 让 pip 工作 所以我可以安装 pyppeteer

但无论我尝试过什么 - 我都无法让 pyppeteer 下载 chromium。我 运行 pyppeteer-install,它只是说正在下载 chromium,但 %appdata% pyppeteer 位置没有任何内容。除了手动下载 chromium 并将其放在正确的位置之外,还有什么方法可以修复它吗?

Pyppeteer 正在使用 urllib3 进行下载并且 urllib3 没有从环境变量中获取配置。

您可以手动下载,然后指定自定义位置:

from pyppeteer import launch

browser = await launch(executablePath='<path>')

参考文献:

基于 pyppeteer/download_chromium 但使用 urllib3.ProxyManager 而不是 urllib3.PoolManager

from io import BytesIO
import urllib3

from tqdm import tqdm
from pyppeteer import chromium_downloader  

def download_zip(url: str) -> BytesIO:
        """Download data with proxy from url."""
        print('Starting Chromium download. Download may take a few minutes.')
    
        with urllib3.ProxyManager(proxy_url='http://proxy-ip:port') as http:
            # Get data from url.
            # set preload_content=False means using stream later.
            r = http.request('GET', url, preload_content=False)
            if r.status >= 400:
                raise OSError(f'Chromium downloadable not found at {url}: Received {r.data.decode()}.\n')
    
            # 10 * 1024
            _data = BytesIO()
            try:
                total_length = int(r.headers['content-length'])
            except (KeyError, ValueError, AttributeError):
                total_length = 0
    
            process_bar = tqdm(total=total_length, unit_scale=True, unit='b')
            for chunk in r.stream(10240):
                _data.write(chunk)
                process_bar.update(len(chunk))
            process_bar.close()
    
        print('Chromium download done.')
        return _data
    
    
    def download_chromium() -> None:
        """Download and extract chromium."""
        chromium_downloader.extract_zip(download_zip(chromium_downloader.get_url()), chromium_downloader.DOWNLOADS_FOLDER / chromium_downloader.REVISION)
        print(f'Chrome executable path: {str(chromium_downloader.chromium_executable())}')

只需在您的程序中调用 download_chromium()

Rem: 别忘了将 http://proxy-ip:port 替换为您的公司代理。