我喜欢学习如何编写 Google Apps 脚本以将 FinViz.com 中的部分表格数据导入 Google 表格,但使用 UrlFetchApp。我曾经使用过 IMPORTHTML。如果只搜索几个股票贴纸,它就可以很好地工作。但是,如果我为许多股票贴纸循环该函数,尽管 FinViz 中提供了此类数据,但我仍面临大量空导入。我从谷歌搜索中看到 UrlFetchApp 是要走的路。任何人都可以帮助使用 UrlFetchApp 编写脚本吗?举一个简单的例子,我在 Google 工作表的 A 列中有一个股票行情列表,并从 FinViz 导入它们的远期市盈率数据,然后将它们写在工作表的 B 列中。我在这里分享一个谷歌表。太感谢了!!
https://docs.google.com/spreadsheets/d/1JmdBKqNVoj2wpJ_st22C8FchgTUss8Edo4q_gMmAgKE/edit?usp=sharing
在对您提供的网站进行了一些测试并尝试了不同的方法之后,我认为您可以更轻松地简单地检索 HTML 静态网站文本,然后在网站上进行 Javascript 搜索以返回您要查找的值。我分享的答案中的代码失败了,因为当您尝试使用 XML 解释该数据时,该网站存在一些 HTML 兼容性问题。此外,您感兴趣的表格位于div
元素内,因此您需要先访问该div
元素,然后再访问该表格。
以下带有不言自明注释的代码返回并记录Forward P/E
您提供的网站中的值:
function ALTERNATIVE(){
// Get all the static HTML text of the website
const res = UrlFetchApp.fetch('https://finviz.com/quote.ashx?t=AAPL', {muteHttpExceptions: true}).getContentText();
// Find the index of the string of the parameter we are searching for
index = res.search("Forward P/E");
// create a substring to only get the right number values ignoring all the HTML tags and classes
sub = res.substring(index+68,index+73);
Logger.log(sub);
return sub;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句