google-sheets - 在 Google 表格中,我可以查询 DXY 吗?
问题描述
我努力了:
=importxml("https://www.wsj.com/market-data/quotes/index/DXY", "//span[@id='quote_val']/text()")
还有其他一些事情,但到目前为止还没有运气!
解决方案
我相信你的目标如下。
- 您想检索
//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]);
}
结果:
参考:
推荐阅读
- c# - 如何修复错误弹出“类未注册”
- apache-zeppelin - Apache Zeppelin 错误本地 jar 不存在
- firebase - 验证对指定经过身份验证的用户的读取访问权限
- java - Maven Build缺少主类
- python - Python:方法 .as_matrix 将在未来版本中删除。改用 .values
- python - 获取包含单个元素的索引错误
- laravel - Laravel 5.6 存储 API 的会话令牌服务器端
- python - 替换字符组合
- javascript - 改变一个组件中的值会影响另一个组件
- vue.js - VueJS:如何滚动 v-list-title