Google Sheets – How to Fetch One HTML Table from a URL

google sheetsimporthtmlimportxml

I'm trying to fetch part of a single table from an HTML page into my Google spreadsheet. Been having lots of problems. importhtml() fails quite regularly (but not always) and simply displays "loading…" forever. So I decided to try UrlFetchApp() instead. Having problem with it as well.

Here is the URL I'm trying to fetch:

http://finance.yahoo.com/quote/COH160916P00041000?p=COH160916P00041000

My importhtml() version is simply:

=index(IMPORTHTML("http://finance.yahoo.com/quote/COH160916P00041000?p=COH160916P00041000","table",1),3,2)

As I mentioned this does work… mostly. But it breaks too often to be dependable. How can I accomplish the same thing using UrlFetchApp()?

I just discovered something very strange related to this. This formula works fine:

=IMPORTHTML("http://finance.yahoo.com/quote/coh160916P00040000","table",1)

This displays "Loading…" indefinitely:

=IMPORTHTML("http://finance.yahoo.com/quote/COH160916P00040000","table",1)

The only difference is the capitalization of "COH" in the second one. That is the correct URL on Yahoo's site:

http://finance.yahoo.com/quote/COH160916P00040000

So what gives?

Best Answer

Turns out importxml() is the way to go without a doubt. Works great!