Category: FUNCTION | Updated: 2026-11-22
How to Use the IMPORTXML Function: Scrape Specific Web Page Elements with XPath
Overview of the IMPORTXML Function
The IMPORTXML function extracts text data from specific tags or attributes on a web page (HTML or XML) at a specified URL using an XPath query. It can be used for lightweight web scraping, such as monitoring competitor prices or collecting news headlines.
Syntax
=IMPORTXML(url, xpath_query)
- url: The URL of the web page from which to fetch data (
"https://..."). - xpath_query: An XPath expression specifying the elements to extract as a text string (e.g.,
"//title"for the page title or"//h1"for headings).
Practical Examples with a Table
This example extracts the title, heading (h1 tag), and link URLs from a web page.
| Cell | A (Target URL) | B (XPath Query) | C (Formula) | D (Sample Extracted Result) |
|---|---|---|---|---|
| 1 | URL | XPath | Formula | Result |
| 2 | https://example.com | //title |
=IMPORTXML(A2, B2) |
Example Domain |
| 3 | https://example.com | //h1 |
=IMPORTXML(A3, B3) |
Example Domain |
| 4 | https://example.com | //a/@href |
=IMPORTXML(A4, B4) |
https://www.iana.org/domains/example |
Common Errors and Precautions
- No JavaScript rendering support:
IMPORTXMLonly reads the initial HTML source, so it cannot retrieve dynamic content rendered later by frameworks like React or Vue. - Double quotes inside XPath: When specifying attributes in XPath, use single quotes (e.g.,
"//div[@class='price']") so they do not conflict with the double quotes enclosing the formula argument. - Login-protected pages and access restrictions: Pages requiring member login authentication or sites with anti-crawler protections (such as WAFs) will return an
#N/Aerror.
Frequently Asked Questions
Q. Why does IMPORTXML say 'Imported content is empty'?
A. This happens when the page is dynamically generated with JavaScript, the website blocks scraping, or the specified XPath query does not match any elements.
Q. How can I find the XPath to use with IMPORTXML?
A. In Chrome Developer Tools (Inspect), right-click the target element and select 'Copy' > 'Copy XPath' to easily obtain it.