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

使用IMPORTXML函数出现“Resource at url not found”错误的原因排查

解决Google Sheets IMPORTXML "Resource at url not found" 错误(Scryfall无结果查询场景)

问题背景

使用=importxml("https://scryfall.com/search?as=grid&order=name&q=type%3Aaetherborn+color%3E%3DW+%28game%3Apaper%29+legal%3Avintage","//strong[1]")时触发“Resource at url not found”错误,期望无匹配卡片时返回“0 cards found”;但结构类似的查询=IMPORTXML("https://scryfall.com/search?as=grid&order=name&q=type%3Aadvisor+color%3E%3DW+%28game%3Apaper%29+legal%3Avintage","//strong[1]")可正常运行,更换XPath也无法解决问题。

原因分析

Scryfall的查询逻辑是:当搜索无匹配卡片时,会返回404 HTTP状态码;而Google Sheets的IMPORTXML函数遇到404状态码时,会直接抛出资源未找到错误,不会尝试解析页面内容。有匹配结果的查询返回200状态码,因此能正常解析XPath获取内容。

解决方案

方案1:自定义Google Apps Script函数(精准处理)

通过脚本捕获HTTP状态码,区分404和其他错误,返回对应结果:

  1. 打开Google Sheets,点击「扩展程序」→「Apps 脚本」
  2. 粘贴以下代码并保存:
function SCRYFALL_CARD_COUNT(query) {
  const baseUrl = "https://scryfall.com/search?as=grid&order=name&q=";
  const encodedQuery = encodeURIComponent(query);
  const fullUrl = baseUrl + encodedQuery;
  
  const response = UrlFetchApp.fetch(fullUrl, {muteHttpExceptions: true});
  const statusCode = response.getResponseCode();

  if (statusCode === 404) {
    return "0 cards found";
  } else if (statusCode === 200) {
    const doc = XmlService.parse(response.getContentText());
    const strongElements = doc.getRootElement().getDescendants().filter(elem => elem.asElement() && elem.asElement().getName() === 'strong');
    return strongElements.length > 0 ? strongElements[0].asElement().getText() : "0 cards found";
  } else {
    return "Error fetching data";
  }
}
  1. 返回表格,在单元格中使用函数:
=SCRYFALL_CARD_COUNT("type:aetherborn color>=W (game:paper) legal:vintage")

方案2:IFERROR兜底(快速但不精准)

用IFERROR捕获所有IMPORTXML错误,统一返回“0 cards found”(无法区分404和其他错误,如网络问题):

=IFERROR(IMPORTXML("https://scryfall.com/search?as=grid&order=name&q=type%3Aaetherborn+color%3E%3DW+%28game%3Apaper%29+legal%3Avintage","//strong[1]"), "0 cards found")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 12:10:10