要超过Google Spreadsheet上的ImportXML限制

roo*_*ie4 3 javascript google-sheets web-scraping google-apps-script custom-function

我现在正在坚持"刮擦问题".特别是我想从网页中提取作者姓名到谷歌电子表格.实际上这个功能=IMPORTXML(A2,"//span[@class='author vcard meta-item']")正在运行,但是在我提高链接数量之后它才开始加载无穷无尽.

所以我研究并发现,这个问题是由于谷歌有一个限制的事实.

有没有人知道超出限制或脚本,我可以"轻松复制"? - 我真的没有编码的预感.

Jos*_*ley 11

我创建了一个自定义导入功能,克服了IMPORTXML的所有限制我在大约800个单元格中使用了这个工作表并且效果很好.

它使用Google Sheet的自定义脚本(工具>脚本编辑器...),并使用正则表达式而不是xpath搜索内容.

function importRegex(url, regexInput) {
  var output = '';
  var fetchedUrl = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  if (fetchedUrl) {
    var html = fetchedUrl.getContentText();
    if (html.length && regexInput.length) {
      output = html.match(new RegExp(regexInput, 'i'))[1];
    }
  }
  // Grace period to not overload
  Utilities.sleep(1000);
  return output;
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以像任何函数一样使用此函数.

=importRegex("https://example.com", "<title>(.*)<\/title>")

当然,您也可以参考细胞.

=importRegex(A2, "<title>(.*)<\/title>")

如果您不想在输出中看到HTML实体,则可以使用此功能.

var htmlEntities = {
  nbsp:  ' ',
  cent:  '¢',
  pound: '£',
  yen:   '¥',
  euro:  '€',
  copy:  '©',
  reg:   '®',
  lt:    '<',
  gt:    '>',
  mdash: '–',
  ndash: '-',
  quot:  '"',
  amp:   '&',
  apos:  '\''
};

function unescapeHTML(str) {
    return str.replace(/\&([^;]+);/g, function (entity, entityCode) {
        var match;

        if (entityCode in htmlEntities) {
            return htmlEntities[entityCode];
        } else if (match = entityCode.match(/^#x([\da-fA-F]+)$/)) {
            return String.fromCharCode(parseInt(match[1], 16));
        } else if (match = entityCode.match(/^#(\d+)$/)) {
            return String.fromCharCode(~~match[1]);
        } else {
            return entity;
        }
    });
};
Run Code Online (Sandbox Code Playgroud)

全部一起…

function importRegex(url, regexInput) {
  var output = '';
  var fetchedUrl = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  if (fetchedUrl) {
    var html = fetchedUrl.getContentText();
    if (html.length && regexInput.length) {
      output = html.match(new RegExp(regexInput, 'i'))[1];
    }
  }
  // Grace period to not overload
  Utilities.sleep(1000);
  return unescapeHTML(output);
}

var htmlEntities = {
  nbsp:  ' ',
  cent:  '¢',
  pound: '£',
  yen:   '¥',
  euro:  '€',
  copy:  '©',
  reg:   '®',
  lt:    '<',
  gt:    '>',
  mdash: '–',
  ndash: '-',
  quot:  '"',
  amp:   '&',
  apos:  '\''
};

function unescapeHTML(str) {
    return str.replace(/\&([^;]+);/g, function (entity, entityCode) {
        var match;

        if (entityCode in htmlEntities) {
            return htmlEntities[entityCode];
        } else if (match = entityCode.match(/^#x([\da-fA-F]+)$/)) {
            return String.fromCharCode(parseInt(match[1], 16));
        } else if (match = entityCode.match(/^#(\d+)$/)) {
            return String.fromCharCode(~~match[1]);
        } else {
            return entity;
        }
    });
};
Run Code Online (Sandbox Code Playgroud)

  • `&gt;(。*?%\ s + apr)` (2认同)

mic*_*ner 0

没有这样的脚本可以超出限制。由于代码是在 Google 机器(服务器)上运行的,所以你不能作弊。有些限制与您的电子表格绑定,因此您可以尝试使用多个电子表格(如果有帮助)。