如何在Google Cloud Puppeteer函数内部传递两个参数?解析以请求JSON

安德鲁

我正在构建一个带有两个参数的应用程序;请求网址和CSS查询选择器。我很难让请求看起来像这样:“ http:// localhost:5000 / scrapeme / us-central1 / scraperSelector?requestURL = https://www.google.com&selector=#hplogo ”。该请求不接受选择器变量,并且返回未定义。

我不太确定自己在做什么错,我尝试了不同的方法,例如request.body或创建对象并将其传递给代码。我已经阅读了Google文档,却找不到在云函数中传递多个参数的好例子。

const admin = require('firebase-admin');
const functions = require('firebase-functions');
const puppeteer = require("puppeteer");
const chalk = require("chalk");

admin.initializeApp();

// for yellow console logging
const checking = chalk.bold.yellow;

// const uri = "http://localhost:5000/scrapeme/us-central1/scraperSelector";
// const appURL = "scrapeme.firebaseapp.com";


exports.scraperSelector = functions.runWith({ memory: '1GB' }).https.onRequest(async(request, response) => {
    // initialize varialbe to request params
    const requestURL = request.query.requestURL;
    console.log("Evaluating " + requestURL);

    let selector = request.query.selector;
    console.log("Evaluating " + selector);

    console.log("Evaluating " + request.originalUrl);

    // Launch a browser
    const browser = await puppeteer.launch({
        headless: true,
        args: ['--no-sandbox', '--disable-setuid-sandbox']
    });

    // Visit the page a get content
    const page = await browser.newPage();

    // Go to requested URL
    await page.goto(requestURL, { waitUntil: 'networkidle0' });
    console.log(checking("Evaluating " + requestURL));

    // find the css selector
    const content = await page.evaluate(() => {
        console.log(JSON.stringify(selector));

        let selectorCSS = document.querySelector(selector).innerText;
        console.log(selectorCSS);

        return selectorCSS;

    },);

    // Send the response
    response.json(content);

});

// Example URL of how request should look
// http://localhost:5000/scrapeme/us-central1/scraperSelector?requestURL=https://www.google.com&selector=#hplogo

我希望输出解析为JSON响应。我正在尝试从页面中抓取单个项目。{“结果”:“ $ 18.41”}

但是,我得到此输出和错误:

评估https://www.google.com

评估

评估/ scrapeme / us-central1 / scraperSelector?requestURL = https://www.google.com&selector=

评估https://www.google.com

函数:错误:评估失败:ReferenceError:在puppeteer_evaluation_script中未定义选择器:2:36

叶文

您必须将selector变量传递evaluate函数。

    //...
    let selector = request.query.selector;
    //...
    const content = await page.evaluate(selector => { // <-- add the `selector` variable.
    console.log(JSON.stringify(selector));

    let selectorCSS = document.querySelector(selector).innerText;
    console.log(selectorCSS);

    return selectorCSS;

    }, selector); // <-- add the `selector` variable

阅读更多文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Google Cloud中请求增加GPU配额

如何在Google Cloud Function中获取原始请求正文?

如何在Google Cloud Storage中串联一百万个文件

如何在Google Cloud SQL上更改postgresql.conf参数

Google Cloud Functions-为什么GCF将两个位置参数传递给我的函数?

如何在Google Cloud中存储图像?

如何在google cloud函数中的python中进行http响应

如何在来源树上使用Google Cloud?

如何在两个Google Cloud Debian实例之间进行SSH

如何在Google Cloud Platform的两个项目之间移动实例?

如何在云函数上恢复Google Cloud Speech API(longRunningRecognize)超时

如何在Google Cloud函数中使用Geofirex

如何在Google Cloud Scheduler(Python)中定义的Google Cloud函数中使用主体参数?

如何在Compute Engine Google Cloud中使用96个CPU?

如何在Google Cloud Functions中获取HTTP请求正文大小?

如何在一个Google Cloud帐户中管理多个GKE项目

如何在Python中将Google Cloud Natural Language实体情感响应转换为JSON / dict?

如何在Google Cloud Datalab中使用Bigquery JSON函数

如何在Google Cloud Platform中的两个VM实例之间使用磁盘

如何在 Google Cloud Platform 中访问 Kubernetes 节点的内部 HTTP 端口

Google Cloud:如何在 Cloud Datalab 中使用 Cloud ML

如何在 Google Cloud 函数 (Firebase) 的快照中获取子快照?

如何在一个存储库中编写多个 Google Cloud Function

如何在 Google Cloud ML Engine 上的 config.yaml 文件中添加用户特定的参数

如何从两个不同的函数传递参数

如何在 Google Cloud 请求中设置最大接收消息长度?

如何在 Google Cloud Functions 中调用 Express 函数?

如何在 Google Cloud Function 上的 Spring Cloud 函数中获取 Pub/Sub 事件的元数据

如何在 Google Cloud Function 中解析 HTML 文件?