【问题标题】:IMPORTHTML not giving expected resultsIMPORTHTML 未给出预期结果
【发布时间】:2021-06-25 02:11:34
【问题描述】:

我正在尝试从数据库网站提取数据,但出现“导入的内容为空”错误。 我使用的公式是 =IMPORTHTML("https://tbcdb.com/?npcs=1","Table",1)

我尝试将 table 和 list 作为第二个参数,并尝试了各种索引值,但我得到了同样的错误。

【问题讨论】:

    标签: html google-sheets


    【解决方案1】:

    是的,内容为空。
    为什么 ?因为您尝试导入的网站上的 <table> 是使用 javascript 创建的,它在加载后运行,所以 google 表格看不到它。
    您可以询问网站所有者是否提供其他视图选项,例如 XML 或 RSS。

    【讨论】:

      【解决方案2】:

      您可以通过捕获包含所有可用信息的列表来恢复数据

        var source = UrlFetchApp.fetch(url).getContentText()
        var jsonString = source.match(/(?<=listview).*/gi)
        eval('listview = '+jsonString[1].substring(1,jsonString[1].length-2))
      

      例如,获取所有信息

      let resultat = []; 
      function decode(url){
        var source = UrlFetchApp.fetch(url).getContentText()
        var jsonString = source.match(/(?<=listview).*/gi)
        eval('listview = '+jsonString[1].substring(1,jsonString[1].length-2))
        getAllData(1,listview,'listview')
        return resultat
      }
      function getAllData(niv,obj,id) {
        const regex = new RegExp('[^0-9]+');
        for (let p in obj) {
          var newid = (regex.test(p)) ? id + '.' + p : id + '[' + p + ']';
          if (obj[p]!=null){
            if (typeof obj[p] != 'object' && typeof obj[p] != 'function'){
              resultat.push([niv, (newid), p, obj[p]]);
            }
            if (typeof obj[p] == 'object') {
              if (obj[p].length){
                resultat.push([niv, (newid), p + '[0-' +(obj[p].length-1)+ ']', 'tableau']);
              }else{
                //resultat.push([niv, (newid), p, 'parent']);
              }
              niv+=1;
              getAllData(niv, obj[p], newid );
              niv-=1
            }
          }
        }
      }
      

      检索表格内的信息

      function tblData(url){
        var source = UrlFetchApp.fetch(url).getContentText()
        var jsonString = source.match(/(?<=listview).*/gi)
        eval('listview = '+jsonString[1].substring(1,jsonString[1].length-2))
        var tbl = []
        tbl.push(['name','minlevel','maxlevel','type','classification','id'])
        listview.data.forEach(function(e){
          //name  minlevel    maxlevel    type    classification  id
          tbl.push([e.name,e.minlevel,e.maxlevel,e.type,e.classification,e.id])
        })
        return tbl
      }
      

      https://docs.google.com/spreadsheets/d/1CkXFJoyarz1xWKaRxyAUwh2fulZncoJ5aYP6K2u9A2c/copy

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-15
        • 2016-01-30
        • 2021-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-07
        相关资源
        最近更新 更多