Web scraping googlesheets中的IMPORTXML函数

Web scraping googlesheets中的IMPORTXML函数,web-scraping,google-sheets,google-sheets-formula,google-sheets-query,google-sheets-importxml,Web Scraping,Google Sheets,Google Sheets Formula,Google Sheets Query,Google Sheets Importxml,使用IMPORTXML函数,是否可以构造一个XPATH查询来提取给定Wikipedia页面的行业值 例如,我想从这个页面中获取的值是“零售”,而在这个页面上,它是“时尚”。试试: =INDEX(IMPORTXML("https://en.wikipedia.org/wiki/Boohoo.com", "//td[@class='category']"), 2, 1) 您希望创建xpath来检索给定Wikipedia页面的行业价值 如果我的理解是正确的,就像其他模式一样,那么使用这个x

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

例如,我想从这个页面中获取的值是“零售”,而在这个页面上,它是“时尚”。

试试:

=INDEX(IMPORTXML("https://en.wikipedia.org/wiki/Boohoo.com", 
 "//td[@class='category']"), 2, 1)

  • 您希望创建xpath来检索给定Wikipedia页面的行业价值
如果我的理解是正确的,就像其他模式一样,那么使用这个xpath的公式呢?请把这看作是几个答案中的一个

=INDEX(IMPORTXML(A1,"//th[text()='Industry']/following-sibling::td"),1,1)
样本配方:
  • xpath是
    //th[text()='Industry']/以下同级::td
  • 在这种情况下,
    https://en.wikipedia.org/wiki/Target_Corporation
    https://en.wikipedia.org/wiki/Boohoo.com
    放在单元格“A1”中
结果:

参考:
补充: 从您的回复中,我知道您希望再添加2个URL。因此,所有URL如下所示

  • https://en.wikipedia.org/wiki/Target_Corporation
  • `
  • `
  • `
问题和解决方法: 对于以上URL,当使用
=IMPORTXML(A1,“//th[text()='Industry']/以下兄弟姐妹::td”)
公式时,
Retail
Fashion
Retail
Travel,将返回服务

当xpath修改为
//th[text()='Industry']/以下同级::td/a
Retail
N/a
N/a
Travel
返回

这是由于以下差异造成的

<tr>
  <th scope="row">Industry</th>
  <td class="category"><a href="/wiki/Travel" title="Travel">Travel</a> services</td>
</tr>
  • xpath是
    //th[text()='Industry']/以下同级::td
    。这是没有修改的
  • 在这种情况下,URL放在单元格“A1”中
  • 检索两个值时,检索第一个值。由此,我使用了
    索引
结果:

这非常有用。谢谢在某些情况下,它会导致奇怪的结果。对于这个页面,它返回一个“未找到url上的资源”错误,即使链接正常,HTML似乎与其他页面具有相同的结构。更大的问题是,对于由多个单词(例如-)描述的行业,公式将所有单词放在相邻单元格的第二个单词之后(而不是将所有单词连接到单个单元格中)。我想知道这些问题是否都能解决。@zgall1谢谢您的回复。我没有注意到您也希望将此公式用于其他URL。这是因为我的技术差。对此我深表歉意。我又为您提供的4个URL添加了一个公式。你能确认一下吗?不幸的是,我认为为了从4个URL中检索值,不能仅使用一个xpath直接检索这些值。所以我在这种情况下使用了一个内置函数。如果这不是你想要的方向,我很抱歉。我为迟来的回复道歉。这对我来说确实有用。@zgall1谢谢你的回复。我很高兴你的问题解决了。也谢谢你。
<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>
=INDEX(IMPORTXML(A1,"//th[text()='Industry']/following-sibling::td"),1,1)