【问题标题】:Scraping data from a table from a specific title value and filter specific lines (Google App Script)从特定标题值的表中抓取数据并过滤特定行(Google App Script)
【发布时间】:2021-08-18 23:53:24
【问题描述】:

CherrioGS 的文档:
https://github.com/tani/cheeriogs

我们的想法是仅从名为 Argentinos Jrs 的表中收集数据,并且不保存 info 列中值为 Away on International duty 的行。

注意:我确实需要根据Argentinos Jrs的值来指定并删除Away on International duty,因为这个表的位置不是固定的,行中的值也是。

我正在寻找的这个例子中的预期结果是这样的:

Carlos Quintana      Mid August
Jonathan Sandoval    Early August

网站链接是这样的:
https://www.sportsgambler.com/injuries/football/argentina-superliga/

我会留下网站当前的图片,因为如果数据发生变化,我的例子的想法就注册了:

我尝试的代码:

function PaginaDoJogo() {
    var sheet = SpreadsheetApp.getActive().getSheetByName('Dados Importados');
    var url = 'https://www.sportsgambler.com/injuries/football/argentina-superliga/';

    const contentText = UrlFetchApp.fetch(url).getContentText();
    const $ = Cheerio.load(contentText);

    $('div:contains("Argentinos Jrs") > div > div.inj-container:not(contains("Away on International duty")) > span.inj-player')
        .each((index, element) => {
            sheet.getRange(index + 2, 1).setValue($(element).text());
        });

    $('div:contains("Argentinos Jrs") > div > div.inj-container:not(contains("Away on International duty")) > span.inj-return.h-sm')
        .each((index, element) => {
            sheet.getRange(index + 2, 2).setValue($(element).text());
        });
}

【问题讨论】:

  • 你的 jquery 表达式不起作用吗?
  • 晚安,提前感谢您的帮助!正确的@Alex,它不起作用。
  • 尝试删除部分 jquery 以找出问题所在。从后面开始,当你得到正确答案时,你发现了一个错误的部分
  • 我仍在学习如何使用选择器元素以与 Cheerio 一起使用,问题是为什么我需要帮助来创建一个有效的选项,因为我使用 not(contains()) 不起作用。
  • 老实说,您不需要在节点中使用 jquery。你可以使用 jsdom 之类的东西,并使用 web 样式的 querySelector 来获得你需要的东西。这将比你正在做的事情容易得多。阅读您的 jquery 查询很痛苦。

标签: javascript google-apps-script web-scraping cheerio


【解决方案1】:
function PaginaDoJogo() {
  const sheet = SpreadsheetApp.getActive().getSheetByName('Dados Importados');
  const url = 'https://www.sportsgambler.com/injuries/football/argentina-superliga/';
  const response = UrlFetchApp.fetch(url);
  const content = response.getContentText();
  const match = content.match(/Argentinos Jrs[\s\S]+?<!--Livestream call to action-->/);
  const regExp = /<div[\s\S]+?<span class="inj-player">(.+?)<\/span>[\s\S]+?<span class="inj-info">(.+?)<\/span>[\s\S]+?<span class="inj-return h-sm">(.+?)<\/span>[\s\S]+?<\/div>/g;
  const values = [];
  while ((r = regExp.exec(match[0])) !== null) {
    // console.log(r[1], r[2], r[3]);
    if (r[1] !== 'Name' && r[2] !== 'Away on International duty') {
      values.push([r[1], r[3]]);
    }
  }
  sheet.getRange(2, 1, values.length, 2).setValues(values);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-04
    • 2023-03-19
    • 2019-11-14
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    相关资源
    最近更新 更多