ShareYourGroupShareYourGroupGoogle Sheets

Web and import

IMPORTHTML, IMPORTXML and IMPORTDATA functions in Google Sheets

IMPORTHTML brings a table or list from a web page into the sheet; IMPORTXML and IMPORTDATA fetch other kinds of web data.

These three functions pull information from the web straight into cells, and fetch it again from time to time so that the sheet stays current. They suit public reference tables: a league table, a list of holidays, exchange rates published by an institution, an open data file.

No results are printed here, because they depend on live pages.

Syntax

=IMPORTHTML(url, query, index)
=IMPORTXML(url, xpath_query)
=IMPORTDATA(url)
ArgumentWhat it is
urlThe address of the page or file, starting with http or https.
queryFor IMPORTHTML: "table" or "list".
indexWhich table or list on the page, counting from 1.
xpath_queryFor IMPORTXML: an XPath expression that says which parts of the page to return.

Examples

Which one?

FunctionFetches
IMPORTHTMLa table or a list from a web page
IMPORTDATAa .csv or .tsv file at a web address
IMPORTXMLany part of a page, chosen by an XPath expression
IMPORTFEEDthe items of an RSS or Atom feed
IMPORTRANGEa range from another Google Sheets file

Start with IMPORTHTML. Most information worth importing is already in a table.

Finding the right table

A page may contain many tables, including ones used only for layout. The third argument says which. There is no label to go by, so try 1, then 2, then 3, until the one you want appears.

IMPORTXML in brief

XPath describes a path to elements of a page.

XPathReturns
//h1every top-level heading
//pevery paragraph
//a/@hrefthe address in every link
//table[1]//trthe rows of the first table
//div[@class='price']elements with a given class
//titlethe page title

It is powerful and takes practice. A browser's Inspect tool shows the structure of a page.

Why an import fails

A cell may show "Loading..." for some seconds. That is normal.

Be fair to the websites you read

Refreshing

Imported data is fetched again periodically while the sheet is open, roughly hourly, though Google does not promise an exact schedule. It is not a live feed. To keep a snapshot, copy the result and paste values only.

Cleaning what arrives

Imported tables often include units, footnote marks and text numbers. Wrap the import in QUERY to choose columns and rows, and use VALUE and REGEXREPLACE to turn text such as "1,250 km" into numbers. Inside QUERY, imported columns are named Col1, Col2, counting from the left.

Wrap the whole thing in IFERROR so that a temporary failure shows a short note and not a block of errors.

Common mistakes

In Excel

Excel has no IMPORTHTML. Its counterpart is Data, Get Data, From Web, part of Power Query, which is set up through menus and refreshed on request. Import formulas become plain values when a Sheets file is saved as Excel.

Related functions

The results on this page were worked out by us from the sample sheet; they were not produced by a script running Google Sheets. If Sheets shows you something different, please tell us.

This is an independent help site. It is not connected with or approved by Google. Google Sheets is a trademark of Google LLC, named here only to say which program a formula is for. The people and figures in the sample sheets are made up.