0
votes

So I'm not a professional programmer, but I'm trying to scrape data off the Reuters homepage and import it into google spreadsheets. I know that there have already been questions answerd about scraping from Reuters, however, that didn't help me.

I want data from this page: http://www.reuters.com/finance/stocks/financialHighlights?symbol=9983.T

specifically, if you scroll down, there's a lot of data on the company's financials, packed into tables. I need specific values out of the tables.

So naturally my question to you is, how can I get specific values out of the tables? For instance, I want the first value out of the line that's labelled "Net Profit Margin (TTM)". The value should be 7.30.

So I got the xpath by using google chrome developer tools, right-click on the element and select "copy xpath". Since I'm not a programmer I dont know any other way for arriving at a specific element from the tables.

I tried the following function in google spreadsheets:

=IMPORTXML(URL as written above,"//*[@id='content']/div[2]/div/div[2]/div[1]/div[13]/div[2]/table/tbody/tr[14]/td[2]")

but it returns

"#N/A - Error, imported content is empty"

What can I do to get the value?

1

1 Answers

2
votes

The IMPORTXML() function of Google Sheets is known to be incredibly buggy and it is not surprising if people dig up real errors in it. Still, we don't know exactly why your original XPath expression does not work.

I want the first value out of the line that's labelled "Net Profit Margin (TTM)". The value should be 7.30.

The path expression you got from the developer tools heavily relies on positioning, and not at all on actual values. If you can rely on the text content of the first cell in this row, use

=IMPORTXML("http://www.reuters.com/finance/stocks/financialHighlights?symbol=9983.T","//tr[contains(td[1],'Net Profit Margin (TTM)')]/td[2]")

which means

Select all tr elements where the text content of the first td child element contains "Net Profit MArgin (TTM)" and select the second td of that tr.

and the result will be

7.3