Google Spreadsheet XPATH:无法提取没有数据/值的元素

时间:2018-05-16 10:46:07

标签: xpath google-sheets

从这个link我想获得排名位置标志,我使用这个获得图像链接:

=IMPORTXML("https://int.soccerway.com/national/england/premier-league/20172018/regular-season/r41547/tables/","//table[@class='leaguetable sortable table detailed-table']//tr/td[@class='direction']/img/@src")

是的,我得到了img url。

https://s1.swimg.net/gsmf/678/img/delta_plus.gif
https://s1.swimg.net/gsmf/678/img/delta_min.gif
https://s1.swimg.net/gsmf/678/img/delta_plus.gif
https://s1.swimg.net/gsmf/678/img/delta_min.gif
https://s1.swimg.net/gsmf/678/img/delta_min.gif
https://s1.swimg.net/gsmf/678/img/delta_plus.gif
https://s1.swimg.net/gsmf/678/img/delta_min.gif

但如何获得内部没有元素的<td class="direction"></td>

某些元素有这样的img标签:

<td class="direction"><img src="https://s1.swimg.net/gsmf/678/img/delta_plus.gif" width="7" height="4" title="Previous rank: 4" alt="Previous rank: 4"></td>

另一个元素里面没有img标签:

<td class="direction"></td>

如何获取内部没有数据的元素,这样我就可以得到这样的列表:

https://s1.swimg.net/gsmf/678/img/delta_plus.gif
https://s1.swimg.net/gsmf/678/img/delta_min.gif



https://s1.swimg.net/gsmf/678/img/delta_plus.gif


https://s1.swimg.net/gsmf/678/img/delta_min.gif
https://s1.swimg.net/gsmf/678/img/delta_min.gif

https://s1.swimg.net/gsmf/678/img/delta_plus.gif
https://s1.swimg.net/gsmf/678/img/delta_min.gif

我该怎么办?感谢

1 个答案:

答案 0 :(得分:1)

您想同时检索<td class="direction"></td><td class="direction"><img ... /></td>。当td没有<img ... />时,您想要放置空行。如果我的理解是正确的,那么这个修改怎么样? https://int.soccerway.com/national/england/premier-league/20172018/regular-season/r41547/tables/放入&#34; A1&#34;。

修改后的公式:

=IMPORTXML(A1,"//td[@class='direction' and not(*)] | //td[@class='direction']/img/@src")
  • //td[@class='direction' and not(*)]表示tdclassdirection且没有子元素。
  • //td[@class='direction']/img/@src表示td具有img的子元素,并检索@src

通过这些,当td的{​​{1}}具有@class='direction'的子元素时,会检索img。 <{1}}的{​​{1}}没有子元素时,将放空。

结果:

enter image description here

注意:

  • 您也可以使用@src

如果我误解了你的问题,我很抱歉。