- 您想要创建 xpath 以检索给定 Wikipedia 页面的行业值。
如果我的理解是正确的,作为其他模式,这个 xpath 的公式怎么样?请认为这只是几个答案之一。
示例公式:
=IMPORTXML(A1,"//th[text()='Industry']/following-sibling::td")
- xpath 是
//th[text()='Industry']/following-sibling::td。
- 在这种情况下,
https://en.wikipedia.org/wiki/Target_Corporation 或 https://en.wikipedia.org/wiki/Boohoo.com 的 URL 放在单元格“A1”中。
结果:
参考:
补充:
根据您的回复,我知道您还想添加 2 个网址。所以所有的网址如下。
问题和解决方法:
对于上述网址,当使用=IMPORTXML(A1,"//th[text()='Industry']/following-sibling::td")的公式时,返回Retail、Fashion、Retail和Travel, services。
当xpath修改为//th[text()='Industry']/following-sibling::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>
和
<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>
由此,我认为不幸的是,为了从上面检索Travel、Retail 和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。
结果: