【发布时间】:2022-01-23 01:06:07
【问题描述】:
我正在使用 Docker 和 docker-compose 运行一个带有 puppeteer 的 NodeJS 抓取工具。我的故障排除如下:
预期输出: 我从维基百科获取标题页
案例 1: 使用 await 运行 function1() 时,进程停止
OUTPUT:
Browser is running
//console.log("function1() end" ) does not execute
案例2: 如果 function1() 没有 await 则该函数不会执行,但 console.log after 会被执行
OUTPUT:
Browser is running
function1() end
如何使用 await 运行 function1 并获取页面输出的标题。
async function function1() {
let page = await browser.newPage()
await page.setUserAgent(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
)
await page.goto(
'https://en.wikipedia.org/wiki/Main_Page',
{
waitUntil: 'networkidle2',
},
)
console.log(await page.title())
}
async function looper() {
await function1()
await console.log('function1 end')
}
async function startPuppeteer() {
browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
})
console.log('Browser is running')
setInterval(looper, 30000)
}
Dockerfile
FROM buildkite/puppeteer:latest
USER root
COPY . /app
RUN cd /app && npm install
EXPOSE 8000
WORKDIR /app
CMD npm run start
Docker-compose.yml
version: "3.9"
services:
web:
build: .
ports:
- "8000:8000"
【问题讨论】:
-
你能显示function1和function2的代码吗?你怎么知道他们没有被处决?你有console.log这些功能吗?
-
在启动 headless chrome 后有一个 console.log 始终运行。刮板功能在没有 docker 的情况下执行,当使用 docker 时,await function1 和 await function2 不执行。此外,我编辑了帖子以显示 console.logs
-
那么 function1 和/或 function2 中一定有一些东西阻止它运行。没有看到代码是不可能提供帮助的。
-
我添加了main函数,谢谢大家的帮助
标签: javascript node.js docker docker-compose puppeteer