Google表格中的IMPORTXML函数

时间:2019-10-31 03:47:11

标签: web-scraping google-sheets google-sheets-formula google-sheets-query google-sheets-importxml

使用@Query(value = "SELECT * FROM table t WHERE validate_row(t) = true", nativeQuery = true) 函数,是否可以构造一个XPATH查询来获取给定Wikipedia页面的行业价值?

例如,我要从此页面中提取的值-https://en.wikipedia.org/wiki/Target_Corporation-是“零售”,而在此页面中,该值-https://en.wikipedia.org/wiki/Boohoo.com-它是“时尚”。

2 个答案:

答案 0 :(得分:2)

  • 您要创建xpath来检索给定Wikipedia页面的行业价值。

如果我的理解是正确的,那么和其他模式一样,使用该xpath的公式又如何呢?请认为这只是几个答案之一。

示例公式:

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

结果:

enter image description here

参考:

已添加:

根据您的回复,我知道您想再添加2个URL。因此,所有URL如下。

问题和解决方法:

对于上述URL,当使用=IMPORTXML(A1,"//th[text()='Industry']/following-sibling::td")的公式时,将返回RetailFashionRetailTravel, 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>

通过这种方式,我认为很不幸,为了从上方检索TravelRetailFashion,仅使用一个xpath不能直接检索它们。因此,在这种情况下,我使用了内置函数。

解决方法:

在此替代方法中,我使用了INDEX。请认为这只是几个答案之一。

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

enter image description here

答案 1 :(得分:0)

尝试:

$storeUpdate = $this->myModel->find($idData);
$storeUpdate->your_field_1=$your_value_1;
$storeUpdate->your_field_2=$your_value_2;
$storeUpdate->save()

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

0