IMPORTXML Function in Google Sheets

IMPORTXML extracts specific parts of a public web page into Google Sheets. Give it a URL and an XPath expression to retrieve headings, link text, link addresses, or other matching elements.

For example, =IMPORTXML("https://example.com","//h1") returns Example Domain. The XPath //h1 tells Sheets to find heading elements named h1.

IMPORTXML Function Syntax

=IMPORTXML(url, xpath_query, [locale])
  • url: A complete web address in quotation marks, or a cell containing one.
  • xpath_query: The XPath expression identifying the elements or attributes to extract.
  • locale: An optional language and region code used when parsing the data. Sheets uses the document locale when you omit it.

The optional locale argument appears in Google’s IMPORTXML documentation. For example, =IMPORTXML(A2,"//h1","en_US") returned the same heading in our test.

When to Use IMPORTXML

Use IMPORTXML when you need particular elements from a page rather than its entire table. It can extract several matches into neighboring cells without copying each item manually.

For an HTML table or list, IMPORTHTML is usually easier. To bring cells from another Google spreadsheet, use IMPORTRANGE.

Import a Page Heading

  1. Enter https://example.com in cell A2.
  2. Select B2 and enter the formula below.
  3. If Sheets displays an external-data banner, review it and click Allow access to enable the import.
=IMPORTXML(A2,"//h1")

The result in B2 is Example Domain. The double slash searches for matching elements anywhere in the document, rather than requiring their full path from the root.

IMPORTXML returns Example Domain from the h1 element at example.com.

Extract Link Text and Link Addresses

To retrieve the text displayed by links, enter this formula in B5:

=IMPORTXML(A2,"//a")

Our example returned Learn more. An a element represents a link, but its visible text is different from its destination address.

To get the address instead, enter this formula in B8:

=IMPORTXML(A2,"//a/@href")

The result was https://iana.org/domains/example. The @href part selects the link’s href attribute. Other pages may return relative paths, so do not assume every result is a complete URL.

Extract Paragraphs and Multiple Matches

Enter the following formula in B11 to select paragraph elements:

=IMPORTXML(A2,"//p")

In the tested page, the first result was its explanatory paragraph. A second result, Learn more, appeared in B12 because the link also sits inside a paragraph element.

One formula can therefore return several cells. Leave the output area empty so Sheets can expand the result. The number of returned matches depends on the current source page.

For a larger page, narrow your XPath to the relevant element or attribute. Ben Collins’ web-scraping tutorial demonstrates how inspecting the source helps identify a useful query.

How IMPORTXML Updates Its Results

Google says IMPORTXML checks for updates hourly while the document is open. Reloading the browser does not force an import refresh; re-entering the formula does.

If Sheets reports a result that is too large, narrow the XPath query. If requests are throttled, reduce repeated imports. See Google’s import-function guidance for these limits.

Other Google Sheets articles you may also like