You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用IMPORTXML导入隐藏div标签数据到Google Sheets

抓取隐藏div导入Google Sheets操作方案

  • 不要直接使用Google Sheets自带的IMPORTHTML/IMPORTXML函数,这类函数仅能拉取站点首次返回的静态HTML源码,无法获取JS动态生成、或者默认设置了隐藏属性的div内容。
  • 优先使用Google Apps Script实现抓取,你只需要打开目标表格,点击「扩展程序」-「Apps Script」,粘贴如下示例代码修改后运行即可:
function getHiddenDivData() {
  const targetUrl = "https://bando.tphcm.gov.vn/ogis";
  // 模拟浏览器请求头,避免被站点拦截
  const requestOptions = {
    headers: {
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    },
    muteHttpExceptions: true
  };
  const response = UrlFetchApp.fetch(targetUrl, requestOptions);
  const fullHtml = response.getContentText();
  // 修改正则匹配你需要的隐藏div的class、id等特征,以下是示例
  const matchRule = /<div class="your-target-class"[^>]*>([\s\S]*?)<\/div>/g;
  const resultList = [];
  let matchedContent;
  while ((matchedContent = matchRule.exec(fullHtml)) !== null) {
    resultList.push(matchedContent[1].trim());
  }
  // 把抓取结果写入当前表格A列
  const activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  resultList.forEach((item, idx) => {
    activeSheet.getRange(idx + 1, 1).setValue(item);
  });
}
  • 如果你运行上述代码还是拿不到目标内容,说明数据是页面加载完成后通过接口异步拉取渲染的,你可以用Puppeteer等无头浏览器工具先拿到完整渲染后的DOM树,再同步到Google Sheets即可,CSS隐藏属性不影响DOM内容的提取。

你提及的相关参考截图:
浏览器开发者工具中的元素
目标待抓取渲染元素

内容的提问来源于stack exchange,提问作者Anh Tokyo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 21:24:03