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

Google Spreadsheet XPath提取:如何获取无内容的direction标签元素

解决IMPORTXML无法获取空<td class="direction">标签的问题

我帮你搞定这个问题!你现在遇到的情况是IMPORTXML只抓取到带<img>的<td>标签,空标签被漏掉了——这是因为你的原XPath只定位到了包含img的td节点,没把空的算进去。要拿到完整的列表,我们需要调整XPath表达式,把两种情况都覆盖到:


解决方案:调整XPath,包含空td节点

我们可以用XPath的|(逻辑或)运算符,把带img的td的src链接和空的td节点合并成一个节点集,这样就能保持原页面的顺序,拿到完整结果。

完整的公式如下:

=IMPORTXML("https://int.soccerway.com/national/england/premier-league/20172018/regular-season/r41547/tables/","//table[@class='leaguetable sortable table detailed-table']//tr/td[@class='direction']/img/@src | //table[@class='leaguetable sortable table detailed-table']//tr/td[@class='direction'][not(img)]")

公式拆解:

  • 第一部分//table[@class='leaguetable sortable table detailed-table']//tr/td[@class='direction']/img/@src:和你原来的逻辑一致,抓取带有img的td的图片链接
  • 第二部分//table[@class='leaguetable sortable table detailed-table']//tr/td[@class='direction'][not(img)]:筛选出没有img子元素的空td节点,[not(img)]是XPath里判断节点不存在的条件

执行这个公式后,你会得到和原页面行数完全匹配的列表:有img的位置显示对应的gif链接,空td的位置显示空白单元格,完美对应所有排名位置标识。


进阶:给空值添加占位符(可选)

如果希望空的位置显示更直观的内容(比如"排名无变化"),可以结合Google Sheets的ARRAYFORMULA和IF函数处理结果:

=ARRAYFORMULA(
  IF(
    IMPORTXML("https://int.soccerway.com/national/england/premier-league/20172018/regular-season/r41547/tables/","//table[@class='leaguetable sortable table detailed-table']//tr/td[@class='direction']/img/@src | //table[@class='leaguetable sortable table detailed-table']//tr/td[@class='direction'][not(img)]")="",
    "排名无变化",
    IMPORTXML("https://int.soccerway.com/national/england/premier-league/20172018/regular-season/r41547/tables/","//table[@class='leaguetable sortable table detailed-table']//tr/td[@class='direction']/img/@src | //table[@class='leaguetable sortable table detailed-table']//tr/td[@class='direction'][not(img)]")
  )
)

注意:这个公式会调用两次IMPORTXML,如果你担心效率,可以先把原始结果存到一个单元格区域,再对该区域做IF判断。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:11:46