How to Use IMPORTXML in Google Sheets: Syntax & Examples
Use the
IMPORTXML function in Google Sheets to fetch data from a webpage by specifying the URL and an XPath query. The syntax is =IMPORTXML(url, xpath_query), where url is the webpage address and xpath_query selects the data you want to import.Syntax
The IMPORTXML function has two parts:
- url: The web page address you want to get data from, inside quotes or a cell reference.
- xpath_query: The XPath expression to select specific data from the page's HTML, also inside quotes.
Example: =IMPORTXML("https://example.com", "//h1") fetches all <h1> headings from the page.
plaintext
=IMPORTXML(url, xpath_query)
Example
This example imports the titles of the top news stories from the Google News homepage. It shows how to use IMPORTXML with a real URL and XPath query.
plaintext
=IMPORTXML("https://news.google.com", "//h3")
Output
Top news story title 1
Top news story title 2
Top news story title 3
...
Common Pitfalls
- Using an incorrect or incomplete XPath query returns
#N/Aor empty cells. - Trying to import data from sites that block scraping or require login will fail.
- Forgetting to put the URL and XPath inside quotes causes formula errors.
- Dynamic content loaded by JavaScript won’t be imported because
IMPORTXMLreads static HTML only.
Wrong: =IMPORTXML(https://news.google.com, //h3) (missing quotes)
Right: =IMPORTXML("https://news.google.com", "//h3")
plaintext
=IMPORTXML(https://news.google.com, //h3) =IMPORTXML("https://news.google.com", "//h3")
Quick Reference
| Parameter | Description | Example |
|---|---|---|
| url | Webpage URL to fetch data from | "https://example.com" |
| xpath_query | XPath to select data from HTML | "//title" |
| Return | Data extracted as a list or table | Page title text |
Key Takeaways
Use IMPORTXML with a URL and XPath query inside quotes to fetch web data.
Ensure your XPath query matches the HTML structure of the target page.
IMPORTXML only works with static HTML, not dynamic JavaScript content.
Always put URL and XPath in quotes to avoid formula errors.
Some websites block data scraping, so IMPORTXML may not always work.