如何使用 Cheerio google 应用脚本从网站 github.com 获取表格

问题描述 投票:0回答:1

我正在尝试使用 Google 应用程序脚本中的 Cheerio lib 从 this 站点获取表格。我在这个答案下面放了一些代码,但只得到了

[] in console.log()

这是我的代码

function test2() {
  const url = 'https://github.com/labnol/apps-script-starter/blob/master/scopes.md';
  const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
  const $ = Cheerio.load(res);
  var data = $('tbody').find('td').toArray().map((x) => { return $(x).text() });
  console.log(data);
}

我还看到一些答案:

  1. 一个

  2. 两个

    但是他们没有给我任何关于如何获得想要的结果的线索

javascript google-apps-script html-parsing cheerio
1个回答
0
投票

你的代码对我有用(在 Ubuntu 22 上的 Node 20.11.1 中测试):

const cheerio = require("cheerio"); // ^1.0.0-rc.12

const url =
  "https://github.com/labnol/apps-script-starter/blob/master/scopes.md";

fetch(url)
  .then(res => {
    if (!res.ok) {
      throw Error(res.statusText);
    }

    return res.text();
  })
  .then(html => {
    const $ = cheerio.load(html);
    const data = $("tbody")
      .find("td")
      .toArray()
      .map(x => $(x).text());
    console.log(data);
  })
  .catch(err => console.error(err));

输出:

[
  'Cloud SQL Admin API v1beta4',
  '',
  'View and manage your data across Google Cloud Platform services',
  'https://www.googleapis.com/auth/cloud-platform',
  'Manage your Google SQL Service instances',
  'https://www.googleapis.com/auth/sqlservice.admin',
  '',
  '',
  'Android Management API v1',
  // ... 1360 total items ...
]

也就是说,有一些空元素可以清理,而且数据太扁平而无法使用——本质上是一大行。我将使用两级或三级表 -> 行 -> 基于单元格的刮擦来保留数据的表格性质并避免将其展平。在这里,由于在撰写本文时只有一张表,因此跳过顶层嵌套并直接抓取行和单元格似乎可以:

const cheerio = require("cheerio"); // ^1.0.0-rc.12

const url =
  "https://github.com/labnol/apps-script-starter/blob/master/scopes.md";

fetch(url)
  .then(res => {
    if (!res.ok) {
      throw Error(res.statusText);
    }

    return res.text();
  })
  .then(html => {
    const $ = cheerio.load(html);
    const data = [...$("tr")].map(e =>
      [...$(e).find("td, th")].map(e => $(e).text().slice(0, 25))
    );
    console.table(data.slice(0, 10));
  })
  .catch(err => console.error(err));

输出(删除切片调用以查看所有数据,无需截断):

┌─────────┬─────────────────────────────┬─────────────────────────────┐
│ (index) │ 0                           │ 1                           │
├─────────┼─────────────────────────────┼─────────────────────────────┤
│ 0       │ 'Google OAuth API Scope'    │ 'Scope Description'         │
│ 1       │ 'Cloud SQL Admin API v1bet' │ ''                          │
│ 2       │ 'View and manage your data' │ 'https://www.googleapis.co' │
│ 3       │ 'Manage your Google SQL Se' │ 'https://www.googleapis.co' │
│ 4       │ ''                          │ ''                          │
│ 5       │ 'Android Management API v1' │ ''                          │
│ 6       │ 'Manage Android devices an' │ 'https://www.googleapis.co' │
│ 7       │ ''                          │ ''                          │
│ 8       │ 'YouTube Data API v3'       │ ''                          │
│ 9       │ 'Manage your YouTube accou' │ 'https://www.googleapis.co' │
└─────────┴─────────────────────────────┴─────────────────────────────┘

此外,最好使用 GitHub 的 API 来拉取文件而不是获取(对你和他们来说)。

© www.soinside.com 2019 - 2024. All rights reserved.