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

谷歌表格:是否有脚本/函数可输入单词获取在线词典释义?

谷歌表格获取在线词典释义的可行方案

一、修正你当前的IMPORTXML用法

你写的IMPORTXML存在两处错误:

  • URL占位符写法错误:应该用&C2&拼接单元格内容,正确的URL格式是"https://dictionary.cambridge.org/dictionary/english/"&C2
  • XPath表达式无效:你写的是DOM操作的JS代码,IMPORTXML要求标准XPath语法。以剑桥词典为例,提取释义的XPath可以用//div[contains(@class, 'def ddef_d db')]/text()

修正后的公式示例:

=IMPORTXML("https://dictionary.cambridge.org/dictionary/english/"&C2, "//div[contains(@class, 'def ddef_d db')]/text()")

二、获取韦氏词典释义的自定义脚本方案

韦氏词典的页面结构更复杂,且容易触发反爬,用IMPORTXML稳定性差,推荐用Google Apps Script写自定义函数:

步骤与代码

  1. 打开谷歌表格,点击「扩展程序」→「Apps Script」
  2. 替换默认代码为以下内容:
function getMerriamWebsterDefinitions(word) {
  if (!word) return "请输入有效单词";
  
  try {
    const url = `https://www.merriam-webster.com/dictionary/${encodeURIComponent(word)}`;
    const response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
    const html = response.getContentText();
    
    // 匹配韦氏词典的释义节点(页面结构若更新需调整正则)
    const regex = /<span class="dtText">:(.*?)<\/span>/g;
    let match;
    const definitions = [];
    
    while ((match = regex.exec(html)) !== null) {
      // 清理HTML标签和冗余空格
      const cleanDef = match[1].replace(/<[^>]+>/g, '').trim();
      cleanDef && definitions.push(cleanDef);
    }
    
    return definitions.length ? definitions.join("\n") : "未找到对应释义";
  } catch (err) {
    return "获取失败:" + err.message;
  }
}
  1. 保存脚本后回到表格,在单元格输入=getMerriamWebsterDefinitions(C2)即可调用

三、关键注意事项

  • 在线词典的页面结构可能随时更新,XPath或正则表达式需要同步调整
  • 频繁请求可能触发网站反爬限制,建议避免批量高频调用
  • 若追求长期稳定,可优先考虑词典官方提供的API(注意权限和费用说明)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 21:39:17