web-scraping - Google 表格中的 IMPORTXML 函数
问题描述
使用该IMPORTXML
函数,是否可以构造一个 XPATH 查询来提取给定维基百科页面的行业值?
例如,我想从这个页面提取的值 - https://en.wikipedia.org/wiki/Target_Corporation - 是“零售”,而在这个页面上 - https://en.wikipedia.org/wiki/Boohoo。 com - 这将是“时尚”。
解决方案
- 您想要创建用于检索给定 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如下。
https://en.wikipedia.org/wiki/Target_Corporation
- ` https://en.wikipedia.org/wiki/Boohoo.com
- ` https://en.wikipedia.org/wiki/Woot
- ` https://en.wikipedia.org/wiki/TripAdvisor
问题和解决方法:
对于上述 URL,当使用公式时=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
.
推荐阅读
- amazon-s3 - 红移卸载问题
- java - java - 如何在SpringBoot的java中为@RequestBody中的每个字段只取一个值
- javascript - 在本地浏览器中按 F5 时,Weinre 如何以及为何刷新客户端浏览器?
- postman - 如何获得特定(高速公路)道路的车道数?
- node.js - 让任务在 59.05 秒后超时 aws lambda nodejs
- tsql - 窗口总和未正确显示数据
- vue.js - Parcel JS VUE 动态图片 :src
- entity-framework - 如何正确使用 EFCore 和 SignalR Core(避免缓存实体)
- html - 在 Squarespace 如何更改横幅图像的位置
- javascript - 使用 Angular 的多选下拉菜单标签