首页 > 解决方案 > Google 表格中的 IMPORTXML 函数

问题描述

使用该IMPORTXML函数,是否可以构造一个 XPATH 查询来提取给定维基百科页面的行业值?

例如,我想从这个页面提取的值 - https://en.wikipedia.org/wiki/Target_Corporation - 是“零售”,而在这个页面上 - https://en.wikipedia.org/wiki/Boohoo。 com - 这将是“时尚”。

标签: web-scrapinggoogle-sheetsgoogle-sheets-formulagoogle-sheets-querygoogle-sheets-importxml

解决方案


  • 您想要创建用于检索给定 Wikipedia 页面的 Industry 值的 xpath。

如果我的理解是正确的,与其他模式一样,这个 xpath 的公式怎么样?请认为这只是几个答案之一。

示例公式:

=IMPORTXML(A1,"//th[text()='Industry']/following-sibling::td")
  • xpath 是//th[text()='Industry']/following-sibling::td.
  • https://en.wikipedia.org/wiki/Target_Corporation在这种情况下,或的 URLhttps://en.wikipedia.org/wiki/Boohoo.com被放在单元格“A1”中。

结果:

在此处输入图像描述

参考:

添加:

从您的回复中,我知道您想再添加 2 个 URL。所以所有的URL如下。

问题和解决方法:

对于上述 URL,当使用公式时=IMPORTXML(A1,"//th[text()='Industry']/following-sibling::td"),返回Retail, Fashion,RetailTravel, services

当 xpath 修改为//th[text()='Industry']/following-sibling::td/a, Retail, #N/A,#N/ATravel返回时。

其原因是由于以下差异。

<tr>
  <th scope="row">Industry</th>
  <td class="category"><a href="/wiki/Travel" title="Travel">Travel</a> services</td>
</tr>

<tr>
  <th scope="row" style="padding-right:0.5em;">Industry</th>
  <td class="category" style="line-height:1.35em;"><a href="/wiki/Retail" title="Retail">Retail</a></td>
</tr>

<tr>
  <th scope="row" style="padding-right:0.5em;">Industry</th>
  <td class="category" style="line-height:1.35em;">Fashion</td>
</tr>

通过这一点,我认为不幸的是,为了检索TravelRetail并且Fashion从上面,那些不能只用一个 xpath 直接检索。所以我为这种情况使用了一个内置函数。

解决方法:

在这个解决方法中,我使用了INDEX. 请认为这只是几个答案之一。

=INDEX(IMPORTXML(A1,"//th[text()='Industry']/following-sibling::td"),1,1)
  • xpath 是//th[text()='Industry']/following-sibling::td. 这没有被修改。
  • 在这种情况下,URL 放在单元格“A1”中。
  • 当检索到 2 个值时,将检索第一个值。通过这个,我使用了INDEX.
结果:

在此处输入图像描述


推荐阅读