代码之家  ›  专栏  ›  技术社区  ›  Digital Farmer

从特定标题值和筛选特定行从表中刮取数据

  •  0
  • Digital Farmer  · 技术社区  · 5 年前

    文件 CherrioGS
    https://github.com/tani/cheeriogs

    其思想是只从具有名称的表中收集数据 Argentinos Jrs Away on International duty info 不保存列。

    阿根廷Jrs 出外执行国际任务 ,因为此表的位置不是固定的,而且值也是以行表示的。

    在这个例子中,我希望得到的结果是:

    Carlos Quintana      Mid August
    Jonathan Sandoval    Early August
    

    网站链接如下:
    https://www.sportsgambler.com/injuries/football/argentina-superliga/

    我将保留站点的当前图像,因为如果数据发生更改,我的示例的想法将被注册:
    enter image description here

    我尝试的代码是:

    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());
            });
    }
    
    0 回复  |  直到 5 年前
        1
  •  2
  •   idfurw    5 年前
    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);
    }