Philip Chow
Philip Chow

Reputation: 73

Google Sheet importxml tr td

https://www.klsescreener.com/v2/stocks/view/5232/leon-fuat-berhad enter image description here

Above is the website that I'm using, wish to import the row P/E, just want to import the value (6). The code that I write is =IMPORTXML("https://www.klsescreener.com/v2/stocks/view/5232/leon-fuat-berhad", "//td[contains(@class,'number')]") it pulls the entire value on the right side however I just want to only select the P/E number which I'm struggling to do so. How to edit the code to only select the P/E value?

Upvotes: 1

Views: 2274

Answers (2)

David Denenberg
David Denenberg

Reputation: 730

This ultimately depends on how many details you are comfortable embedding in your XPath expression regarding the document's structure and literals. For example this retrieves your P/E value of 6:

=IMPORTXML("https://www.klsescreener.com/v2/stocks/view/5232/leon-fuat-berhad", "//table[contains(@class,'stock_details')]//td[@title='Price to Earning Ratio']/following-sibling::td[1]/text()")

With an XPath expression that first selects the table that contains the "stock_details" class and then selects a td within the table that has title="Price to Earning Ratio" and then selects the text of the immediately next (first following) td element.

Or you could identify that following td (with the 6) using class as you originally tried:

=IMPORTXML("https://www.klsescreener.com/v2/stocks/view/5232/leon-fuat-berhad", "//table[contains(@class,'stock_details')]//td[@title='Price to Earning Ratio']/following-sibling::td[@class='number']/text()")

and other similar approaches depending on which literal/structure assumptions you're comfortable with.

Upvotes: 1

Kin Siang
Kin Siang

Reputation: 2699

Another method to extract the value using index and xml path

=index(IMPORTXML("https://www.klsescreener.com/v2/stocks/view/5232/leon-fuat-berhad", "//div[@class ='table-responsive']//table[1]/tbody/tr[9]/td[2]"),1)

Upvotes: 1

Related Questions