从 URL 加载网页作为 html 以使用 Firebase 函数构建价格跟踪器

Loading a webpage from a URL as an html to build a price tracker using Firebase Functions

首先我对这种类型的开发非常陌生

我正在尝试使用 firebase 云功能构建价格跟踪器以从网页进行跟踪

假设我要跟踪此产品: https://www.jarir.com/sa-en/apple-magic-keyboard-mouse-combo-547294.html

  1. 如何使用 typeScript lang 在我的云函数项目中加载此页面并将其作为 html 或字符串保存在 const 中?

这是我使用 pubsub 每 30 分钟触发一次此事件:

import * as functions from 'firebase-functions';
import admin = require('firebase-admin');

export const priceTracker = functions.pubsub.schedule('every 30 minutes')
.onRun(async(context) => {

    // 1- How to load this page: https://www.jarir.com/sa-en/apple-magic-keyboard-mouse-combo-547294.html 
    // and save it in a `const` as an html or string 

    // 2- How to extract the price from the html const and save the result into my firestore database
});

提前致谢

这确实是一个广泛的话题,很难在 Stack Overflow 的答案中完全回答。我要做的是帮助您将其分解为更小的步骤,并为这些步骤提供 links。

在所有这些中,认识到这一点确实很有帮助:

  1. Cloud Functions 大部分只是由 Google 的机器管理的小型节点模块。所以如果你想在 Cloud Functions 中做一些事情,请考虑如何在 Node.js.
  2. 中做
  3. Node.js 实际上只是服务器上的 JavaScript 运行。因此,如果您想在 Node.js 中做一些事情,请考虑如何在“普通旧 JavaScript”中进行。

考虑到这一点:

  1. 正在从 URL 将 HTML 页面加载到您的 Cloud Functions 代码中

    正在从 URL 中搜索 加载 HTML 页面 Node.js 似乎有一些不错的结果,包括 Get URL Contents in Node.js with Express.

  2. 在 HTML 中查找价格归结为解析 HTML。 link 似乎是开始的好地方:Extracting table value from an URL with Node JS, with many more good results by searching for Parsing a HTML page from a URL in Node.js

  3. 最后:按照 adding data to the database.

    上的文档将生成的 HTML 或值存储到 Firestore 中应该相当简单