Google Sheet中ImportXML无法找到正确XPath获取数据求助
Let's break down why your XPath isn't working and fix it step by step:
Why your current XPath fails
The XPath you copied from browser dev tools is based on the rendered DOM (after JavaScript runs and the browser adds elements like <tbody> automatically). But Google Sheets' ImportXML fetches the raw HTML source of the page, which often doesn't include <tbody> tags for tables (browsers insert them dynamically even if they're not in the original code). That's why your formula returns #N/A—it's looking for a <tbody> that doesn't exist in the raw HTML.
Fixes to try
Here are adjusted XPaths that should work with the raw HTML:
Remove the
<tbody>reference (most likely fix):=ImportXML("https://siteurl.com","//*[@class='span6']/table/tr[1]/td[2]/strong")Browsers inject
<tbody>into tables automatically, but it's rarely present in the original page source. Removing this part aligns your XPath with whatImportXMLactually sees.Simplify the XPath to avoid strict hierarchy:
If the first fix still doesn't work, try targeting the table by its class instead of the parentspan6container, which might be more reliable:=ImportXML("https://siteurl.com","//table[contains(@class,'table-bordered')]/tr[1]/td[2]/strong")Verify the raw HTML structure:
Double-check the page's raw source (right-click the page → "View Page Source") to confirm the exact path. Search for the<strong>element you want—this will show you the real hierarchy without browser-injected elements.
Why ImportHTML works
ImportHTML ignores strict DOM hierarchy and just scans the raw HTML for tables by index, so it can find the table even if the exact XPath is off. But ImportXML requires precise matching to the raw HTML structure, which is why adjusting your XPath to match the source code is key.
内容的提问来源于stack exchange,提问作者hardy




