Going Stellar

Home/Google Sheets reference

Google Sheets function · Web and import

IMPORTXML in Google Sheets: syntax and examples

IMPORTXML downloads a page or feed and returns the nodes that an XPath query selects. It works on the HTML a server sends, so it is good for titles, links, prices and any text that is already in the source. Results change when the page changes.

Syntax

IMPORTXML(url, xpath_query)

Arguments

ArgumentTypeRequiredWhat it does
urlstringyesThe page or feed address, with http:// or https://. A cell reference works.
xpath_querystringyesAn XPath expression that picks the nodes to return, for example "//h1".

Examples

The examples use this table, starting in A1:

https://en.wikipedia.org/wiki/Google_Sheets
=IMPORTXML(A1, "//title")

Result: Google Sheets - Wikipedia

The page title, read from the URL stored in A1.

Run in a Google Sheet (English US locale) on . The result changes over time; this is the value on the run date.

=IMPORTXML("https://en.wikipedia.org/wiki/Google_Sheets", "//h1")

Result: Google Sheets

The text of the main heading.

Run in a Google Sheet (English US locale) on . The result changes over time; this is the value on the run date.

=IMPORTXML(A1, "//h1/@id")

Result: firstHeading

An attribute instead of text: the id of the h1 element.

Run in a Google Sheet (English US locale) on . The result changes over time; this is the value on the run date.

=INDEX(IMPORTXML(A1, "//h2"), 1)

Result: Contents

The first of many matches. IMPORTXML returns a column, INDEX picks one cell.

Run in a Google Sheet (English US locale) on . The result changes over time; this is the value on the run date.

=COUNTA(IMPORTXML(A1, "//h2"))

Result: 8

How many second-level headings the page has right now.

Run in a Google Sheet (English US locale) on . The result changes over time; this is the value on the run date.

Common errors

  • A query that matches nothing returns #N/A. Check the XPath against the page source, not the rendered page.
  • Content that a script adds after loading is not in the downloaded HTML, so IMPORTXML cannot see it.
  • The URL needs the protocol. Plain text such as "not a url" returns #N/A, the same error as an empty match.
  • Each call is a web request, so many IMPORTXML formulas in one file make it slow to recalculate.

Compared with Excel

Excel has no IMPORTXML. The nearest tools are WEBSERVICE with FILTERXML on Windows, or Power Query.

Sources

  1. IMPORTXML (Docs Editors Help)

Markdown version · All functions