如何在Google Sheets的IMPORTXML中用*匹配多子类class属性?
用正则风格匹配批量获取Forexfactory日历元素属性
需求说明
需从Forexfactory日历页面批量获取三类td元素下span的title属性,三类td的class分别为:
calendar__cell calendar__impact impact calendar__impact calendar__impact--lowcalendar__cell calendar__impact impact calendar__impact calendar__impact--mediumcalendar__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
使用步骤
- 将上述XPath公式填入任意单元格(例如A1)
- 在其他单元格执行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
相关产品推荐
相关产品推荐

