【问题标题】:How to get table content using either apps script or importxml?如何使用应用程序脚本或 importxml 获取表格内容?
【发布时间】:2021-03-31 12:02:26
【问题描述】:

我一直在尝试使用下面的公式获取以下信息,但它显示为无限期加载:

=SUBSTITUTE(index(IMPORTHTML("https://finviz.com/quote.ashx?t="&A5&"","table",INDEX(6,1)),3,1),"",FALSE,TRUE)

由于我有超过一千行,我不确定多次调用这个 importHtml 是否会起作用......

[![在此处输入图片描述][1]][1]

价值观 能源 |石油和天然气勘探与生产 |美国是我想要得到的!

谢谢! [1]:https://i.stack.imgur.com/XvubH.png

【问题讨论】:

  • 能问一下“A5”的值吗?
  • 是的,@Tanaike!这就是“MIME”!谢谢!
  • 感谢您的回复。不幸的是,我无法从https://finviz.com/quote.ashx?t=MIME 的 URL 的 HTML 中找到 Energy | Oil & Gas E&P | USA 的值。对此我深表歉意。
  • 你能用“OVV”作为值试试吗,@Tanaike?
  • 感谢您的回复。从你的回复中,我提出了一个答案。你能确认一下吗?关于Since I got like over a thousand rows, I'm not sure that this calling this importHtml that many times will work...,我不确定以上示例是否可以用于您的情况。所以,当我的回答不能用于你的情况时,我很抱歉。

标签: google-apps-script xpath google-sheets


【解决方案1】:

我相信你的目标如下。

  • 您想从https://finviz.com/quote.ashx?t=OVV 检索Energy | Oil & Gas E&P | USA 的值。
  • 您想从https://finviz.com/quote.ashx?t=MIME 检索Technology | Software - Infrastructure | United Kingdom 的值。
  • 您希望使用 IMPORTXML 和 Google Apps 脚本来实现此目的。

示例公式:

示例 xpath:

//div[@class='content']//table[1]//td[@class='fullview-links']

示例公式:

=JOIN("",IMPORTXML(A1,"//div[@class='content']//table[1]//td[@class='fullview-links']"))
  • 在这种情况下,URL 被放到单元格“A1”中。

结果:

示例脚本:

在此脚本中,请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中。并且请将=SAMPLE("https://finviz.com/quote.ashx?t=OVV") 的自定义函数放到一个单元格中。这样,脚本就运行了。

function SAMPLE(url) {
  const res = UrlFetchApp.fetch(url).getContentText();
  const content = res.match(/<div class\="content">[\s\S\w]+?<\/div>/g);
  if (content) {
    const table = [...content[0].matchAll(/<table[\s\S\w]+?<\/table>/g)][0][0];
    if (table) {
      return [...table.matchAll(/<tr[\s\S\w]+?<\/tr>/g)].pop()[0].replace(/<[\w\s\S]+?>/g, "").replace(/&amp;/g, "&");
    }
  }
  return "No value";
}
  • 此示例脚本返回与上述示例公式相同的结果。

参考资料:

【讨论】:

  • 完美!谢谢!
  • @Antonio Santos 感谢您的回复。很高兴您的问题得到解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多