【问题标题】:Google Sheets Import XML text value will not displayGoogle 表格导入 XML 文本值不会显示
【发布时间】:2020-09-10 16:30:40
【问题描述】:

我正在尝试将一些能源数据导入 Google 表格,但似乎无法显示该值,因为它只是 HTML 行中的文本,并且在输出。我已尝试将 /text() 添加到 XPath,但仍然无法正常工作。

要导入数据的网站:

http://www.caiso.com/TodaysOutlook/Pages/default.aspx

Google 表格中的公式:

=IMPORTXML("http://www.caiso.com/TodaysOutlook/Pages/default.aspx","//div[@class='overview-large-number']")

还尝试了以下 Xpath 并显示相同的 --,---。

/html/body/div[2]/div[3]/div[4]/div/div/div[2]/div/div[1]/div[1]/div[1]
/html/body/div[2]/div[3]/div[4]/div/div/div[2]/div/div[1]/div[1]/div[1]/text()

网页截图:

Caiso Webage with Data

带有数据的 HTML 元素:

<div class="overview-large-number">41,946 <span>MW</span></div>

Google 表格当前输出的屏幕截图:

Google Sheets Screenshot

非常感谢您提供任何见解。

【问题讨论】:

    标签: html xpath web-scraping google-sheets google-sheets-importxml


    【解决方案1】:

    数据在页面加载后填充,因此您不能使用ImportXML()。数据通过 URL http://www.caiso.com/outlook/SP/stats.txt 上的 HTTP Get 请求,并以 JSON 格式返回。

    1. here 获取 ImportJSON 脚本并将其添加到您的工作表(工具 > 脚本编辑器)。
    2. 这应该足以让您入门:
    =Transpose(ArrayFormula(HLOOKUP(
    {"AvailableCapacity","CurrentSystemDemand","todayForecastPeakDemand","histDemand","TodaysPeakDemand","tomorrowsForecastPeakDemand"},
    ImportJSON("http://www.caiso.com/outlook/SP/stats.txt"),2,0)))
    

    请注意,历史峰值已经是一个字符串,因此您可能需要做一些额外的工作才能得到这个数字。

    【讨论】:

    • 非常感谢。我添加了脚本文件,这个解决方案有效。您怎么知道在哪里可以找到带有数据请求的 URL?供我参考,因为它是一个 .aspx 网址,所以您知道它需要在当前页面之外请求信息吗?
    • @Cory 我在浏览器中打开了网络监视器 (F12) 并寻找 XHR。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多