Home > Back-end >  In XPATH I use '''...and''' to add an additional criterion, in cheerio
In XPATH I use '''...and''' to add an additional criterion, in cheerio

Time:03-04

I use the cheeriogs library to work in Google App Script:
id library: 1ReeQ6WO8kKNxoaA_O0XEQ589cIrRvEBA9qcWpNqdOP17i47u6N9M5Xh0

Using =IMPORTXML('url','xpath') I make the call with this XPATH:

//div[contains(@class,'match-card') and ../../td[@class='score-time ']/a[contains(@href, 'matches')]]

The idea is to collect the div that contain the @class with the word match-card
BUT
he needs to have td linked to @class='score-time' and a contains the @href with the word matches

I tried to find a way to do this with CHEERIOGS but it always returns blank, my attempts were:

    $('tr:contains(td.score-time > a[href^="/matches/"]) > div[class^="match-card"]')
                  .each((index, element) => {ss.getRange(index   2, 2).setValue($(element).text().trim());});
    $('tr td.score-time:contains(a[href^="/matches/"]) > div[class^="match-card"]')
                  .each((index, element) => {ss.getRange(index   2, 2).setValue($(element).text().trim());});
    $('tr td.score-time > a[href^="/matches/"] > div[class^="match-card"]')
                  .each((index, element) => {ss.getRange(index   2, 2).setValue($(element).text().trim());});

How I could go about achieving my expected result?

Additional information via requests in the comments:

Example link:

enter image description here

CodePudding user response:

In your situation, how about the following modified script?

Modified script:

const values = [];
$("tr.expanded").each((_, element) => {
  var v1 = $(element).find('span').attr('data-value');
  var v2 = $(element).find('td.score-time > a').attr('href');
  if (v2) {
    values.push([Utilities.formatDate(new Date(Number(v1) * 1000), Session.getScriptTimeZone(), "HH:mm"), v2]);
  }
});
console.log(values);

// If you want to put the values to the Spreadsheet, please use this. `ss` from your showing script.
ss.getRange(2, 2, values.length, values[0].length).setValues(values);

Result:

When this script is run for the URL of https://int.soccerway.com/national/finland/suomen-cup/20212022/2nd-round/r67751/, the following result is obtained.

[
  ["19:00","/matches/2022/03/27/finland/suomen-cup/oujk/roi-united/3751971/"],
  ["21:30","/matches/2022/03/27/finland/suomen-cup/puleward-city/tornion-pallo-47/3751969/"],
  ["07:00","/matches/2022/03/30/finland/suomen-cup/rastaala/valtti/3751935/"],
  ["07:00","/matches/2022/03/30/finland/suomen-cup/hapk/japs-ii/3751944/"],
  ["07:00","/matches/2022/03/30/finland/suomen-cup/japs-o35/finland-kumu-junior-team/3751949/"],
  ["07:00","/matches/2022/03/30/finland/suomen-cup/harjun-potku/sc-riverball/3751963/"],
  ["07:00","/matches/2022/03/30/finland/suomen-cup/fcs/jyvaskylan-seudun-palloseura/3751965/"],
  ["07:00","/matches/2022/03/30/finland/suomen-cup/barca/kuopion-elo/3751967/"],
  ["07:00","/matches/2022/03/30/finland/suomen-cup/pallo-kerho-37/pupa-o35/3751968/"]
]
  • Related