IMPORTXML Function in Google Sheets
Imports data from any XML, HTML, or RSS/Atom URL using an XPath expression. More precise than IMPORTHTML when you need a specific element.
Syntax
IMPORTXML(url, xpath_query)Returns: A spilled array of values matching the XPath query.
Excel equivalent: Power Query 'From Web' with M language transforms.
Parameters
| Name | Required | Description |
|---|---|---|
| url | Required | The URL to fetch. |
| xpath_query | Required | An XPath expression selecting the elements you want. |
Examples
Page title
=IMPORTXML(A1, "//title")Returns the <title> tag content of the page at URL in A1.
All H2 headings
=IMPORTXML(A1, "//h2")Spills every H2 heading into separate cells.
Specific element by class
=IMPORTXML(A1, "//div[@class='price']")Gets text of any div with class "price". XPath supports rich selectors — see W3Schools for syntax.
When to use an alternative
- IMPORTHTML — You just want a full table or list, not a specific element.
- IMPORTDATA — Source is CSV/TSV.
- Apps Script + XmlService — You need authentication or complex post-processing.
Common errors and how to fix them
#N/A
Cause: XPath doesn't match anything, or the page returned an error.
Fix: Test the URL manually. Test the XPath using a browser extension like "Path Finder" against the page's source.
Returns wrong nodes
Cause: XPath too greedy or too restrictive.
Fix: Refine the XPath. Use Chrome DevTools → Console: $x("your-xpath") to test live against the page.
Related functions
Frequently Asked Questions
Why does my IMPORTXML return #N/A on a page that I can see has data?
Three common reasons: (1) the data is JavaScript-rendered (Google's crawler only sees initial HTML), (2) the page blocks bots, (3) your XPath doesn't match. Test in Chrome DevTools console with $x("your-xpath") to confirm the XPath matches.
Can IMPORTXML scrape sites that require login?
No — IMPORTXML hits the URL with no authentication. For authenticated sources, write an Apps Script with UrlFetchApp that handles login headers, and write the results to a sheet.