You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Cheeriogs在Google Apps Script中提取两元素间的指定行文本

问题原因

你获取不到值的核心原因是nextSibling是原生DOM节点的属性,而Cheeriogs中$()方法返回的是Cheerio封装后的集合对象,不能直接调用原生DOM属性,所以返回空值。

解决方法

推荐优先用文本匹配的方案,容错性更高,不会因为DOM结构微小变动就失效:

// 提取td下p标签的全部文本内容
const pText = $('td > p').text();
// 正则匹配目标数值
const volumeMatch = pText.match(/Total volume for this match in Betfair:\s*(\d+)/);
if (volumeMatch) {
  // 取匹配到的数值写入表格
  sheet.getRange(1, 1).setValue(volumeMatch[1]);
}

如果你的页面DOM结构固定,一定要基于br标签位置提取,可以按如下方式调整原有代码:

// 用get(0)获取原生DOM节点
const targetBr = $('td > p br:nth-child(2)').get(0);
if (targetBr?.nextSibling) {
  // 原生文本节点用nodeValue取内容
  const targetText = targetBr.nextSibling.nodeValue.trim();
  const volume = targetText.match(/\d+/)[0];
  sheet.getRange(1, 1).setValue(volume);
}

内容的提问来源于stack exchange,提问作者Digital Farmer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 13:45:01