我无法使用 xpath 和 IMPORTXML 访问网站中的数据,因为代码片段在源数据之外

里卡多

我想从以下站点获取数据:https : //www.portaldefinancas.com/framecdi.htm

我正在使用带有 IMPORTXML 函数的谷歌表。我对 xpath 有了一些了解,我相信我的问题不是语法。

问题是该网站似乎不希望人们从中获取数据。

我需要从源代码中得到的一段代码如下:

... </fieldset><div class="column50"><script src="js-gen-ctb/tb-tb.js"></script>
Taxas CDI - Mensal - Anual - Acumulada - 2021
<script src="js-tx-ctb/th-cdib.js"></script>
<script src="js-tx/cdib-2021.js"></script> ...

如您所见,在数据应该出现的确切位置有很多脚本。当我访问这些脚本时,它们内部唯一的东西是一段 document.write 代码,如下所示:

document.write(""),document.write('<table cellspacing="1" cellpadding="3" id="tb"><caption><p class="cp">');

这些脚本的唯一功能是将源代码分成不同文件的多个部分。

这样,我无法访问数据,因为函数 IMPORTXML 返回 #NA 错误,因为数据在源代码之外。

在这种情况下,有什么方法可以使用函数 IMPORTXML 获取数据?如何使用此公式访问 .js 文件中的数据?

谢谢

迈克·斯蒂尔森

可以通过解析js文件中html表的内容来获取数据,如下

function getTaxas(url) {
  var source = UrlFetchApp.fetch(url).getContentText()
  source = source.split('document')[2]
  var table = '<table><tr><td' + source.match(/(?<=td).*(?=td)/g) + 'td></tr></table>'
  var doc = XmlService.parse(table);
  var rows = doc.getDescendants().filter(function(c) {
    var element = c.asElement();
    return element && element.getName() == "tr";
  });
  var data = rows.slice(0).map(function(row) {
    return row.getChildren("td").map(function(cell) {
      return cell.getValue();
    });
  });
  return data; 
}

https://docs.google.com/spreadsheets/d/18yhDYr91ORrofV20FMWYmYnwwnGK0QaQguG9uLdP5co/copy

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

需要使用xpath和beautifulsoup从网站抓取数据

IMPORTXML内的XPATH在查询中带有撇号

importxml xpath div如何在没有类和id的情况下获取信息?

Google表格中的importxml xpath

XPath,用于在Google表格中使用ImportXML的<address>标记

Google表格和XPath中的IMPORTXML

Google表格中的ImportXML XPath URL

如何在Google表格中使用IMPORTXML和Xpath捕获<img src> URL?

使用Google表格进行网页抓取。Importxml函数xpath校正

Google表格importxml中XPath中的多个索引

如何使用IMPORTXML和XPath返回特定的链接地址

使用ImportXML提取URL和锚点

无法使用Google表格中的IMPORTXML检索xml数据

如何使用importxml和xPath加载此“标签”

Googlesheets功能-Yahoo Finance中IMPORTXML Xpath对列文本的困难

在Google Spreadsheet中将ImportXML与XPath结合使用的Phantom <span>元素

Xpath查询帮助Google ImportXML

使用xpath和php分组数据值

importXML中的Xpath命令

谷歌表importxml xpath查询

Google电子表格中的Xpath或Importxml

使用 ImportXML 和 XPath 从 XML 中提取数据

importXML 中的 xpath: //comment() vs //*/comment()

无法使用 importxml / xpath 抓取背景图片 url

如何制作正则表达式以与 xPath 和 ImportXML 一起使用?

xpath 获取 IMPORTXML 公式中 2 个标签之间的文本

使用 XPath Google 表格 importxml 函数从字典中获取单词的含义

无法在 div 中使用 importxml 和 xpath 导入文本

无法使用 Google Sheet 中的 IMPORTHTML 或 IMPORTXML 从网站抓取数据