在 XPATH 中我使用 '''...and'''要添加一个额外的标准,在cheerio中有这个选项吗?

发布于 2025-01-11 13:08:26 字数 1687 浏览 0 评论 0原文

我使用 Cheeriogs 库在 Google App 脚本中工作:
id 库: 1ReeQ6WO8kKNxoaA_O0XEQ589cIrRvEBA9qcWpNqdOP17i47u6N9M5Xh0

使用 =IMPORTXML('url','xpath') 我使用此 XPATH 进行调用:

//div[contains(@class, '匹配卡')和../../td[@class='score-time ']/a[contains(@href, 'matches')]]

这个想法是收集 div包含 @class 以及单词 match-card
但是
他需要将 td 链接到 @class='score-time' 并且 a 包含 @href单词 matches

我试图找到一种方法来用 CHEERIOGS 做到这一点,但它总是返回空白,我的尝试是:

    $('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());});

我如何才能实现我的预期结果?

通过评论中的请求提供更多信息:

示例链接:

https://int.soccerway.com/national/finland/suomen-cup/20212022/2nd-round/r67751/

预期结果是当嵌入蓝色链接时收集红色值:

在此处输入图像描述

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:

https://int.soccerway.com/national/finland/suomen-cup/20212022/2nd-round/r67751/

The expected result is to collect the values in red when there are links embedded in blue:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜吻♂芭芘 2025-01-18 13:08:26

根据您的情况,以下修改后的脚本怎么样?

修改后的脚本:

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);

结果:

当针对 https://int.soccerway.com/national/finland/suomen-cup/20212022/2nd-round/r67751/ 的 URL 运行此脚本时,出现以下结果得到结果。

[
  ["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/"]
]

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/"]
]
云之铃。 2025-01-18 13:08:26

这将是 :has 伪:

td.day:has(~ td.score-time a[href*="matches"])

这可能在您使用的版本中不可用

That would be the :has pseudo:

td.day:has(~ td.score-time a[href*="matches"])

This might not be available in the version you're using though

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文