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

Google Sheets IMPORTXML导入失败:无法定位目标表格路径

Troubleshooting Google Sheets IMPORTXML for lotopolonia.com Table Data

First, let's break down why your XPath queries might be returning "imported content is empty" and fix the path issue, then touch on the "new data top" feature you need.

Common Issues & Fixes for XPath Paths

1. Check if <tbody> exists in the raw HTML

Google Sheets' IMPORTXML parses the raw server-side HTML, not the browser-rendered DOM. Browsers automatically add a <tbody> tag to tables even if it's missing from the source code—but the raw HTML from lotopolonia.com doesn't include it explicitly.

To confirm this:

  • Right-click the target page → Select "View Page Source" (not "Inspect")
  • Search for table_01 in the source code. You'll see the table structure has no <tbody> tag defined, which means all your XPath queries including /tbody/ are invalid for the raw HTML.

2. Working XPath Examples

Try these corrected formulas (they target the actual static table structure):

  • To get all cells from the first "second_row" entry:
    =IMPORTXML("http://lotopolonia.com/tabel/arhiva/index.php","//table[@class='table_01']/tr[@class='second_row'][1]/td")
    
  • To fetch all rows and cells from the table:
    =IMPORTXML("http://lotopolonia.com/tabel/arhiva/index.php","//table[@class='table_01']/tr/td")
    
  • To get cells with the "colon2" class specifically:
    =IMPORTXML("http://lotopolonia.com/tabel/arhiva/index.php","//table[@class='table_01']/tr[@class='second_row']/td[@class='colon2']")
    

3. What if the table is dynamically loaded?

If the above still returns empty, the table might be loaded via JavaScript after the initial page load (IMPORTXML can't parse JS-rendered content). In that case, use Google Apps Script to fetch and parse the data manually. Here's a starter snippet:

function fetchLotopoloniaData() {
  const url = "http://lotopolonia.com/tabel/arhiva/index.php";
  const response = UrlFetchApp.fetch(url);
  const html = response.getContentText();
  
  // Extract table content with regex (you can use the Cheerio library for cleaner parsing)
  const tableMatch = html.match(/<table class="table_01">(.*?)<\/table>/s);
  if (!tableMatch) return;
  
  const rows = tableMatch[1].match(/<tr class="(first_row|second_row)">(.*?)<\/tr>/g) || [];
  const data = rows.map(row => {
    const cells = row.match(/<td[^>]*>(.*?)<\/td>/g) || [];
    return cells.map(cell => cell.replace(/<[^>]+>/g, "")); // Strip HTML tags from cells
  });
  
  // Write parsed data to your sheet (adjust the sheet name as needed)
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
  sheet.clearContents();
  if (data.length > 0) {
    sheet.getRange(1, 1, data.length, data[0].length).setValues(data);
  }
}

You can set a time-driven trigger in Apps Script to run this function twice daily for automatic updates.

Implementing "New Data Top" Feature

Once you have the data imported, make new entries appear at the top using these methods:

  1. SORT Function: If your raw data is in Sheet1, create a new sheet and use:
    =SORT(Sheet1!A:Z, 1, FALSE)
    
    This sorts by the first column in descending order—adjust the column number if your date/unique ID is in a different column.
  2. Reverse Array Formula: For full control over reversing the dataset:
    =ARRAYFORMULA(INDEX(Sheet1!A:Z, ROW(Sheet1!A:Z)-ROW(Sheet1!A1)+COUNTA(Sheet1!A:A), COLUMN(Sheet1!A:Z)))
    
    This flips the dataset so the newest rows (added to the bottom of Sheet1) appear at the top.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:17:28