1

This function seems to work in general, but in this case:

URL: http://www.londonstockexchange.com/exchange/prices/stocks/summary/fundamentals.html?fourWayKey=GB00B1YW4409GBGBXSET1

XPATH: //*[@id='leftTable']/table[1]/tbody/tr[23]/td[2] (although it doesn't work with any XPATH).

I get the error

Error: Imported XML content cannot be parsed.

I think the problem is with the page itself, but I cannot see what Google is seeing (or can I?) Any pointers would be much appreciated.

Rubén
  • 34,714
  • 9
  • 70
  • 166
cjm2671
  • 18,348
  • 31
  • 102
  • 161
  • 1
    Sure that IMPORTXML can import HTML - which is not necessarily well-formed XML? Did you try IMPORTHTML? And, to be sure, try an XPath expression _without_ `tbody`. – Mathias Müller Jan 07 '15 at 16:51
  • So IMPORTHTML does work, but all XPATH's seem to fail. Could it be a problem with the source URL? – cjm2671 Jan 08 '15 at 16:26
  • Is what you're trying to find really in the original source HTML or generated by Javascript? Did you try an expression without `tbody`? How did you identify the path expressions? – Mathias Müller Jan 08 '15 at 16:29
  • Yeah it's definitely in there. I tried a couple of generic XPATHs as well (get the links, get the headings), these didn't work either. I identified the path expression I'm using using Chrome's debug tools. – cjm2671 Jan 08 '15 at 16:36
  • Nowadays the URL in this post returns a blank page when JavaScript is disabled. – Rubén Jan 06 '23 at 02:00

2 Answers2

2

Incredibly odd, but the general solution for problems regarding the IMPORTXML function in the new version of Google Sheets is - use the older version.

There, if the following formula is used in a cell:

=IMPORTXML("http://www.londonstockexchange.com/exchange/prices/stocks/summary/fundamentals.html?fourWayKey=GB00B1YW4409GBGBXSET1","//*[@id='leftTable']/table[1]/tbody/tr[23]/td[2]")

The displayed result is:

8.10p

Which is correct, I assume.

Community
  • 1
  • 1
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
1

Most ImportXML errors today are because the page is using Javascript.

If you use Developer Tools and inspect the site in Chrome, you find the HTML which was rendered into the DOM by the Javascript. BUT if you look at the the page source, you won't find the data.

Google Sheets doesn't act as a client browser so there is nowhere for the JS to execute.

This is why the copied XML path from Inspect Element into Google Sheets IMPORTXML errors out.

Bruce Seymour
  • 1,520
  • 16
  • 24
  • It is true in general that DOM elements generated by JS cannot be found with such simple Google sheets functions. But in this case, I don't think that was the problem 7 years ago on www.londonstockexchange.com, since simply using an older version of Google sheets worked fine. – Mathias Müller Jun 21 '22 at 07:46