【问题标题】:IMPORTXML Formula in Google SheetsGoogle 表格中的 IMPORTXML 公式
【发布时间】:2021-09-14 01:30:14
【问题描述】:
我无法使用 Google 表格的 IMPORTXML 函数从“每股账面价值”中提取财务数据
=ImportXML("https://stockanalysis.com/stocks/bby/statistics/", [Xpath])
X 路径:
//*[@id="main"]/div[2]/div[2]/div[4]/table/tbody/tr[6]/td[1]/span
【问题讨论】:
标签:
xml
web-scraping
google-sheets
xpath
google-sheets-formula
【解决方案1】:
您可以按如下方式更改 xpath,并使用元素和文本描述符的关系:
=ImportXML("https://stockanalysis.com/stocks/bby/statistics/", "//span[text()='Book Value Per Share']/parent::td/following-sibling::td[1]")
【解决方案2】:
您不能使用 import xml,因为该页面是使用 javascript 构建的。可以解析网页中包含的json
=bookValuePerShare(A1)
带有自定义功能
function bookValuePerShare(url) {
var source = UrlFetchApp.fetch(url).getContentText()
var jsonString = source.split('<script id="__NEXT_DATA__" type="application/json">')[1].split('</script>')[0]
var data = JSON.parse(jsonString)
result = data.props.pageProps.data.balance.data[5][2]
return result
}