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

如何在Google Sheets的IMPORTXML中用*匹配多子类class属性?

用正则风格匹配批量获取Forexfactory日历元素属性

需求说明

需从Forexfactory日历页面批量获取三类td元素下span的title属性,三类td的class分别为:

  • calendar__cell calendar__impact impact calendar__impact calendar__impact--low
  • calendar__cell calendar__impact impact calendar__impact calendar__impact--medium
  • calendar__cell calendar__impact impact calendar__impact calendar__impact--high

当前单个class的XPath查询可成功,但希望一次性匹配三类class,尝试正则通配符*时语法错误,需用正则风格方式实现批量匹配,不使用contains方法。

解决方案

Google Sheets的IMPORTXML支持通过XPath的matches()函数结合正则表达式实现批量匹配。对应的XPath语句如下:

//td[matches(@class, 'calendar__impact--(low|medium|high)')]//span/@title

使用步骤

  1. 将上述XPath公式填入任意单元格(例如A1)
  2. 在其他单元格执行Google Sheets公式:
=IMPORTXML("https://www.forexfactory.com/calendar?month=jul.2023", A1)

语法说明

  • matches(@class, '正则表达式'):校验td的class属性是否匹配指定正则模式
  • calendar__impact--(low|medium|high):匹配class中包含calendar__impact--low、calendar__impact--medium或calendar__impact--high的内容,|表示逻辑或,正则中的括号用于分组匹配,实现多条件批量匹配

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 01:18:27