首页 > 解决方案 > 在 Google 表格中,我可以查询 DXY 吗?

问题描述

我努力了:

=importxml("https://www.wsj.com/market-data/quotes/index/DXY", "//span[@id='quote_val']/text()")

还有其他一些事情,但到目前为止还没有运气!

标签: google-sheets

解决方案


我相信你的目标如下。

  • 您想检索//span[@id='quote_val']/text()from的值https://www.wsj.com/market-data/quotes/index/DXY

问题和解决方法:

=IMPORTXML("https://www.wsj.com/market-data/quotes/index/DXY","//*")被放入单元格的时候,确认Could not fetch url: https://www.wsj.com/market-data/quotes/index/DXY发生了错误。但是当我使用 Google Apps Script 的 UrlFetchApp 测试 URL 时,我确认可以检索到 HTML 数据。

在这个答案中,作为一种解决方法,我建议您使用使用 Google Apps 脚本的自定义函数来实现您的目标。

示例脚本:

请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中,然后保存。当你使用这个脚本时,请放到=SAMPLE("https://www.wsj.com/market-data/quotes/index/DXY")一个单元格中。这样,脚本将运行并检索值。

function SAMPLE(url) {
  const html = UrlFetchApp.fetch(url).getContentText();
  const res = html.match(/<span id="quote_val">(.+?)<\/span>/);
  if (!res) throw new Error("Value cannot be retrieved.")
  return isNaN(res[1]) ? res[1] : Number(res[1]);
}

结果:

在此处输入图像描述

参考:


推荐阅读