Google Sheets用ImportXML导入属性数据为空,请求故障排查
Hey there, let's break down why your ImportXML formula is returning empty results when trying to grab that data-data attribute from the Rubber Turkey page. Here are the most likely culprits and fixes:
1. The Data is Loaded Dynamically (Most Common Issue)
RuneScape Wiki uses JavaScript to load a lot of dynamic content—including price data. Google Sheets' ImportXML can only parse static HTML (the raw code sent by the server before any JS runs).
To verify this:
- Right-click the page and select View Page Source
- Press
Ctrl+F(orCmd+Fon Mac) to search fordata-data - If you don’t find the attribute in the static source, it’s generated by JavaScript after the page loads.
ImportXMLcan’t access this, so you’ll need an alternative like Google Apps Script to fetch and parse rendered content.
2. Your XPath Expression is Incorrect
If the data-data attribute does exist in the static source, your XPath might be targeting the wrong element or using invalid syntax.
For example:
- A generic XPath to grab all
data-dataattributes from divs would be://div[@data-data]/@data-data - To target the specific price-related div (RS Wiki's price boxes have unique classes), use something like:
//div[contains(@class, "infobox-item-price")]/@data-data
Double-check that cell A3 contains a full, valid XPath (e.g., it starts with // to search the entire document, not just child nodes of the current context). You can also validate XPath in Chrome DevTools: right-click the target element → Inspect → Copy → Copy XPath.
3. Website Blocking ImportXML's Request
Some sites block requests from Google Sheets' default user-agent (since it’s a known bot). To get around this, use Google Apps Script’s UrlFetchApp with a custom user-agent to mimic a browser request. Here’s a quick example:
function getRubberTurkeyPriceData() { const url = "http://runescape.wikia.com/wiki/Rubber_turkey"; const options = { headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36" } }; const response = UrlFetchApp.fetch(url, options); const html = response.getContentText(); // Extract data-data with regex (use XmlService for more robust parsing if needed) const regex = /div[^>]+data-data="([^"]+)"/; const match = html.match(regex); if (match) { SpreadsheetApp.getActiveSheet().getRange("B3").setValue(match[1]); } else { SpreadsheetApp.getActiveSheet().getRange("B3").setValue("No data found"); } }
Quick Debug Checklist
- Confirm the
data-dataattribute exists in the page’s static source - Validate your XPath using browser DevTools
- If dynamic content is the issue, switch to Google Apps Script or a JS-rendering tool.
内容的提问来源于stack exchange,提问作者Ron Phillips

