HEETS 中的 IMPORTHTML 不再起作用了吗?

问题描述 投票:0回答:1

=QUERY(IMPORTHTML("https://www.naturalstattrick.com/playerteams.php?fromseason=20212022&thruseason=20232024&stype=2&sit=5v5&score=all&stdoi=std&rate=y&team=ALL&pos=S&loc=B&toi=0&gpfilt=none&fd=&td=&tgp =410&lines=single&draftteam=ALL","表格",1),"选择Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col33,Col14")

一年以来,这一直有效,但突然间它说“无法获取”。我还可以使用 ImportXML 获取相同的数据吗?

google-sheets web-scraping google-sheets-formula
1个回答
0
投票

关于你的情况,我确认了同样的情况。看来 IMPORTHTML 和 IMPORTXML 不能用于您的 URL。但是,幸运的是,当我使用 Google Apps Script 的 UrlFetchApp 测试您的 URL 时,我确认可以获取 HTML。在这个答案中,我想提出一个示例脚本来检索您的期望值。

示例脚本:

请将以下脚本复制并粘贴到脚本编辑器中。并且,请设置要放置值的工作表名称。另外,请在高级 Google 服务中启用 Sheets API。在此示例中,使用 Sheets API 解析 HTML 表格。我认为 Sheets API 作为 HTML 表格的解析器非常强大。所以我用了它。

顺便说一句,在此脚本中,从公式中的

SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col33,Col14
中,检索列
[2, 3, 4, 5, 6, 7, 8, 9, 33, 14]
。如果要更改请修改
columns

function myFunction() {
  const url = "https://www.naturalstattrick.com/playerteams.php?fromseason=20212022&thruseason=20232024&stype=2&sit=5v5&score=all&stdoi=std&rate=y&team=ALL&pos=S&loc=B&toi=0&gpfilt=none&fd=&td=&tgp=410&lines=single&draftteam=ALL";
  const columns = [2, 3, 4, 5, 6, 7, 8, 9, 33, 14];

  const sheetName = "Sheet1"; // Please set your sheet name.

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(sheetName).clearContents() || ss.insertSheet(sheetName);
  SpreadsheetApp.flush();
  const res = UrlFetchApp.fetch(url);
  const html = res.getContentText();
  const table = html.match(/<table[\w\s\S]*?<\/table>/)[0];
  const requests = { requests: [{ pasteData: { html: true, data: table, coordinate: { sheetId: sheet.getSheetId() } } }] };
  Sheets.Spreadsheets.batchUpdate(requests, ss.getId());
  const values = sheet.getDataRange().getValues().map(r => columns.map(i => r[i - 1] || null));
  sheet.clearContents().getRange(1, 1, values.length, values[0].length).setValues(values);
}

测试:

运行此脚本,得到以下结果。

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.