Google Sheets IMPORTXML XPath - Imported Content is empty

余生颓废 提交于 2020-08-06 06:06:50

问题


I'm attempting to parse the 'PEG Ratio' value of a stock from Yahoo Finance into a Google Sheet, but seeing an error.

URL used: https://finance.yahoo.com/quote/ABBV/key-statistics?p=ABBV

Cell Expression used: =IMPORTXML("http://finance.yahoo.com/quote/ABBV/key-statistics?p=ABBV", "//td[@data-reactid='132']")

Error: '#N/A' value (Error: Imported Content is empty)

Value expected is 1.28 (at the time of posting this query) - from Yahoo Finance > Statistics tab > PEG Ratio table (td has a, attribute data-reactid='132' that I have attempted to filter in the query)

Can anyone help please? Here is a link to the sheet: Google Sheet


回答1:


Issue

IMPORTXML can only read the HTML source of a website. Therefore, those elements and components of a website added dynamically will not be able to be retrieved by the IMPORTXML and thus IMPORTXML will interpret the tag to be with empty content.

Possible workaround

Sometimes, in the Javascript files of the website, you can find out the URL of the source of data being inserted dynamically but that is a tedious task to achieve.

Other option to get the desired value is to use other web scraping techniques.

I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)




回答2:


This is probably not what you want, but I was searching around, and found a Google Sheets Add-On that does manage to pull the "1.28" value from that page. It is free for doing a very limited number of queries per month. If interested, search for IMPORTFROMWEB in the GSuite Marketplace.

I only plugged in your URL and the same XPath that you used, so I was very surprised when the data showed up. No idea how it works.

I apologise if mentioning an Add-On is not appropriate on SO. But knowing that an add-on can get that data off the web page may encourage some other ideas on how to do it natively with Sheets.



来源:https://stackoverflow.com/questions/62973264/google-sheets-importxml-xpath-imported-content-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!