現在、firebaseのcloud functionを利用して、スクレイピングする機能を実装しようとしています。
SPAのサイトのボタンを押すと、cloud functionのコードが発火し、スクレイピングした結果を返すイメージです。
#スクレイピングのための利用ライブラリはpuppeteerです。
puppeteerを立ち上げるための設定で、以下記述をすれば問題ないかと思いましたがエラーとなります。
なお、以下コードをtrueにすると問題なく実行可能です。
headless: false
どのようにすれば本番環境でも目に見える形でスクレイピングできるように実装できるでしょうか?アドバイスいただけると助かります。
(デバッグのために目に見えるようにしたいわけでは無いです。利用者に何が起きているのかを認知させるためにあえてヘッドレスをオフにしたいのです。)
■エラー内容(ブラウザ上に表示されます)
Error: could not handle the request
■コード(cloud function)
import * as functions from "firebase-functions";
const puppeteer = require("puppeteer");
let browser: any = "";
let page: any = "";
const startPuppeteer = async () => {
browser = await puppeteer.launch({
args: [
"--disable-gpu",
"-–disable-dev-shm-usage",
"--disable-setuid-sandbox",
"--no-first-run",
"--no-sandbox",
"--no-zygote",
"--single-process",
],
headless: false,
});
page = await browser.newPage();
};
const usePageInPuppeteer = async (url: string) => {
await page.goto(url);
await page.screenshot();
return await page.title();
};
const closePuppeteer = async () => {
return await browser.close();
};
const runtimeOpts: any = {
timeoutSeconds: 500,
memory: "2GB",
};
exports.helloWorld = functions
.region("asia-northeast1")
.runWith(runtimeOpts)
.https.onRequest((request, response) => {
//response.send()
startPuppeteer()
.then(() => {
return usePageInPuppeteer("https://www.google.com");
})
.then((returnUse) => {
console.log(returnUse);
return response.send(returnUse);
})
.then(() => {
return closePuppeteer();
})
.catch((error) => {
throw new Error(error);
});
});