如何用IMPORTXML将<section id="data-container">内容导入Google Sheets?
Hey there, let's break down why your IMPORTXML formulas aren't working and how to fix this. First, let's address the XPath syntax errors, then tackle the bigger issue of dynamic content loading on that page.
1. Fixing Your XPath Syntax
Your original XPath expressions have small syntax mistakes that would prevent them from working even if the content was static:
- Wrong:
//section/[@id='data-container']→ The extra slash aftersectionis invalid. It should be//section[@id='data-container'](selects the<section>element with id="data-container"). - Wrong:
//section/*[@id='data-container']→ This looks for a child element of<section>with that id, but thedata-containeris the section itself, so the*is unnecessary. - Wrong:
//section/data-container→ This tries to select a<data-container>tag inside<section>, which doesn't exist—you need to use@idto target attributes.
The correct XPath to target that section would be:
//section[@id='data-container']
Or even simpler:
//*[@id='data-container']
But even with the right XPath, you still won't get the enterprise list—here's why.
2. The Real Issue: Dynamic Content Loading
The Inc.com 5000 EU list is loaded dynamically with JavaScript. When you use IMPORTXML, it only fetches the initial static HTML of the page, which doesn't include the actual enterprise data. The data is loaded later by the page's JavaScript code, so IMPORTXML can't see it.
YouTube tutorials for Wikipedia work because Wikipedia tables are in the static HTML—no JS needed to load them.
3. Solutions to Import the Data
Here are two reliable ways to get this data into Google Sheets:
Option 1: Use the Website's API (Easiest)
Most modern sites load dynamic data via an API. Here's how to find and use it:
- Open the Inc.com list page in Chrome, right-click → Inspect to open DevTools.
- Go to the Network tab, then refresh the page.
- Click the XHR filter to see only API requests.
- Look for requests that return JSON data with enterprise details (look for names like
list,data, orinc5000). - Once you find the correct API URL, copy it.
- In Google Sheets, use a custom function like
IMPORTJSON(install it first via the Google Workspace Marketplace) to import the JSON data. For example:
This will pull the structured data directly into your sheet.=IMPORTJSON("your-copied-api-url")
Option 2: Use Google Apps Script to Fetch Dynamic Content
If you can't find the API, you can use Google Apps Script to simulate loading the page (including executing JavaScript) and extract the data. Here's a basic template:
- Open your Google Sheet, go to Extensions → Apps Script.
- Replace the default code with this:
function importInc5000EU() { // First, try to find the API URL as described above—this is faster than parsing HTML const apiUrl = "PASTE-THE-API-URL-YOU-FOUND-HERE"; const response = UrlFetchApp.fetch(apiUrl); const data = JSON.parse(response.getContentText()); // Get the active sheet const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); sheet.clear(); // Clear existing data // Write headers if (data.length > 0) { const headers = Object.keys(data[0]); sheet.getRange(1, 1, 1, headers.length).setValues([headers]); // Write rows of data const rows = data.map(item => headers.map(header => item[header] || "")); sheet.getRange(2, 1, rows.length, headers.length).setValues(rows); } } - Paste the API URL you found into the
apiUrlvariable. - Save the script, then run it (you'll need to grant permissions the first time).
Important Notes
- Always check the site's Terms of Service before scraping or using their API—make sure you're allowed to import the data.
- Avoid making too many rapid requests to the API, as this could get your IP blocked.
- If the API requires authentication (unlikely for public lists), you might need to adjust the script to include headers from the DevTools request.
内容的提问来源于stack exchange,提问作者hccavs19

