我想在docker内部使用puppeteer,但任何网站的https版本都出现错误
我的dockerfile:
FROM node:14.8.0-slim
ENV http_proxy http://10.156.10.155:3128
ENV https_proxy http://10.156.10.155:3128
RUN apt-get update \
&& apt-get install -y wget gnupg \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install -y nano vim google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
RUN npm config set proxy http://10.156.10.155:3128
RUN npm config set https-proxy http://10.156.10.155:3128
RUN npm i puppeteer@5.2.1
ADD screenshot.js /app/screenshot.js
RUN groupadd -r pptruser && useradd -r -g pptruser -G audio,video pptruser
RUN chown -R pptruser:pptruser /app
USER pptruser
WORKDIR /app
# CMD ["node", "index.js"]
和screenshot.js:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless: true,
ignoreHTTPSErrors: true,
args: ['--disable-dev-shm-usage',
'--proxy-server=http://10.156.10.155:3128',
'--no-sandbox',
'--disable-setuid-sandbox'
]
});
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Windows NT 6.1; rv:60.7) Gecko/20100101 Firefox/60.7');
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})().catch(error => console.trace(error));
对于http://example.com,我没有错误,但是对于https版本,我有此错误:
Trace: Error: net::ERR_TUNNEL_CONNECTION_FAILED at https://example.com
at navigate (/node_modules/puppeteer/lib/cjs/puppeteer/common/FrameManager.js:113:23)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async FrameManager.navigateFrame (/node_modules/puppeteer/lib/cjs/puppeteer/common/FrameManager.js:88:21)
at async Frame.goto (/node_modules/puppeteer/lib/cjs/puppeteer/common/FrameManager.js:405:16)
at async Page.goto (/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:826:16)
at async /app/screenshot.js:19:3
at /app/screenshot.js:23:29
at processTicksAndRejections (internal/process/task_queues.js:97:5)
我尝试设置http_proxy和https_proxy env,但没有成功。
我刚刚尝试过不带docker的东西,所以与docker没有关系:/
你有什么主意吗?
答案 0 :(得分:0)
这绝对是您的代理服务器设置的问题,而不是伪娘或docker。不幸的是,我无权访问您的代理,因此无法复制它。但是首先想到的是-使用https代理时通常必须进行身份验证。您可以在进行导航之前使用amt_cents = amt * 100.0 + 0.5;
传递用户名和密码:
page.authenticate
这些通常是一些域凭据。
我想到的另一件事是证书错误,但我已经page.authenticate({username:'user', password:'password'});
见到您了:)
如果身份验证不起作用,建议您打开Chrome并在其中检查代理设置-也许您还缺少其他功能?
答案 1 :(得分:0)
对于那些有相同问题的人,请在puppeteer.launch中使用选项--user-agent:
const browser = await puppeteer.launch({
headless: true,
ignoreHTTPSErrors: true,
args: ['--disable-dev-shm-usage',
'--proxy-server=http://10.156.10.155:3128',
'--user-agent="Mozilla/5.0 (Windows NT 6.1; rv:60.7) Gecko/20100101 Firefox/60.7"',
'--no-sandbox',
'--disable-setuid-sandbox'
]
});