问题
this is the link i want to do my importxml on
https://sg.finance.yahoo.com/quote/D05.SI
<div class="dot-label Pos(r) Fz(13px) Fw(500) D(ib) Ta(c)"><span>Current</span><!-- react-text: 18 --> <!-- /react-text --><span>25.08</span></div>
i would like to use importxml to retrieve the value 25.08.
=IMPORTXML("https://sg.finance.yahoo.com/quote/D05.SI","//*[@class='dot-label Pos(r) Fz(13px) Fw(500) D(ib) Ta(c)']//span")
but it always return #NA, please advise the correct syntax or link and the reasons so i can have some understanding.
回答1:
Using id attribute selector is always safer than using class attribute selector, because they are using class binding and it gets changed frequently. I found the closest DOM that has id attribute, and lookup until the current price is scraped.
You should try this xpath:
=INDEX(IMPORTXML("https://sg.finance.yahoo.com/quote/D05.SI","//div[@id='quote-header-info']/div[last()]/div[1]"),1)
But sometimes the layout / DOM structure get changed, you might need to check and update the xpath to ensure that it correctly scrape the value that you wanted.
来源:https://stackoverflow.com/questions/47752009/xpath-google-sheet-importxml