簡體   English   中英

超過 Google 電子表格上的 ImportXML 限制

[英]To exceed the ImportXML limit on Google Spreadsheet

我現在陷入了“刮擦問題”。 特別是我想從網頁中提取作者的姓名到谷歌電子表格。 實際上,函數=IMPORTXML(A2,"//span[@class='author vcard meta-item']")正在工作,但是在我增加要抓取的鏈接數量后,它開始無限加載。

所以我研究並發現,這個問題是由於谷歌的限制。

有沒有人知道超過限制或腳本,我可以“輕松復制”? - 我真的沒有編碼的預感。

我創建了一個自定義導入函數,它克服了 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;
}

然后,您可以像使用任何函數一樣使用此函數。

=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;
        }
    });
};

大家一起…

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;
        }
    });
};

沒有這樣的腳本可以超出限制。 由於代碼在 Google 機器(服務器)上運行,因此您不能作弊。 某些限制綁定到您的電子表格,因此您可以嘗試使用多個電子表格(如果有幫助)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM