【发布时间】:2020-06-05 13:38:29
【问题描述】:
我正在尝试从以下website (https://www.fundsquare.net/security/information?idInstr=275136) 解析数据
我想在 Google 表格中显示基金的价格。但是,当使用“importxml”函数时,我收到“导入的内容为空”的错误。有人知道我能做些什么来修复它吗?
我尝试该功能的方式:
=IMPORTXML("https://www.fundsquare.net/security/summary?idInstr=275136" ,"//*[@class ='surligneorange']" )
=IMPORTXML("https://www.fundsquare.net/security/information?idInstr=275136" , "//*[@id='content']/table[2]/tbody/tr/td[3]/span[1]")
=IMPORTXML("https://www.fundsquare.net/security/information?idInstr=275136" , "//*[@id='content']//span[1]")
我不断收到同样的错误。在寻找这个错误时,我得到了静态数据和动态数据之间的区别。这个数据会发生变化,所以我猜它是动态的,但我不确定这会如何影响公式。
我一直在尝试使用脚本编辑器做一些事情,但没有成功。还尝试使用 RegExp 进行一些操作,但除了示例之外没有任何其他内容。我对抓取的了解有限,因此非常感谢尝试解析数据时的任何提示和技巧! 任何帮助将不胜感激!
编辑: 在脚本编辑器中,我尝试了以下代码:
function importdata() {
var found, html, content = '';
var response = UrlFetchApp.fetch("https://www.fundsquare.net/security/information?idInstr=275136");
if (response) {
html = response.getContentText();
if (html) content = html.match(/<span class="surligneorange">(.*)<\/span>/)[0];
}
Logger.log(content);
}
这给了我以下日志输出:
[20-06-05 07:44:58:529 PDT] <span class="surligneorange">31.15 EUR</span> <span style="color:#DD0000;text-align:left;padding:4px 0;"> -0.67 % <img src="/images/share/variationNegative.gif" style="vertical-align:middle;"/></span></td></tr></table><div id="onglet"><a href="/security/documents?idInstr=275136">Documents</a><a href="/security/eusd?idInstr=275136">Taxes</a><a href="/security/histo-divid?idInstr=275136">Dividends</a><a href="/security/histo-prices?idInstr=275136">Hist. Prices</a><a href="/security/price?idInstr=275136">Price</a><a href="/security/order-ref-data?idInstr=275136">Order Ref. Data</a><a class="selected" href="/security/information?idInstr=275136">Security Information</a><a href="/security/summary?idInstr=275136">Overview</a><br class="clear_r"/></div><div id="blocresume"><table class="portlet100pct" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" class="portletleft50pct"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" class="portletBordGris"><div style="position: relative; left: 1px;" class="bloctitle"><img src="/Fundsquare/images/share/x.gif" border="0" height="1" width="1" /></div><DIV class="bloctitle" style="position: relative; top: -21px; right: 1px;"><span style="top: 3px;" >General information</span>
值 31.15 是我要抓取的值。如何在电子表格中获取此值?
编辑 06/06 10:14: 更多问题
能否请您帮助我了解您所做的更改。我尝试匹配的和你匹配的到底有什么区别。
我的:
if (html) content = html.match(/<span class="surligneorange">(.*)<\/span>/)[0];
你的:
if (html) content = html.match(/<span class="surligneorange">([\d.]*).*?<\/span>/)[1];
and:
if (html) content = html.match(/<span class="surligneorange">([\d.]*).*<\/span>/)[1];
我的[0] 和你的[1] 有什么区别。是您只要求第一个值吗?
我的.*和你的([\d.]*).*或[\d.]*).*?有什么区别???
我对 javascript 的了解不是很好,所以我不确定它的作用。谢谢您的帮助!
【问题讨论】:
标签: javascript html parsing google-apps-script google-sheets