此作品有效:
=importxml("https://discgolfmetrix.com/?u=scorecard&ID=900113&view=result", "//table[@class='data data-hover']/tr/td[2]")
这失败了:
=importxml("https://discgolfmetrix.com/?u=scorecard&ID=1172639&view=result", "//table[@class='data data-hover']/tr/td[2]")
如果这是我能理解的另一种方式,因为第一个有2个tbody标签。
答案 0 :(得分:1)
GoogleSheets以自己的方式解析页面(父>>子结构与浏览器中的结构并不完全相同)。在XPath中使用//tr
来避免解析错误:
=IMPORTXML("https://discgolfmetrix.com/?u=scorecard&ID=1172639&view=result","//table[@class='data data-hover']//tr/td[2]")
或使用IMPORTHMTL
和QUERY
:
=QUERY(IMPORTHTML("https://discgolfmetrix.com/?u=scorecard&ID=1172639&view=result","table",1),"select Col2 OFFSET 1")
EDIT
:更多详细信息:
对于第一个链接,解析后的HTML结构如下:
<table>
<tr>
<td></td>
<td>your_data</td>
...
</tr>
<tr>
<td></td>
<td>your_data</td>
...
</tr>
...
</table>
您的XPath正常工作。
对于第二个链接,有一个前面的tbody
元素,其中包含tr
元素。结构是:
<table>
<tbody>
<tr>
<td></td>
<td>your_data</td>
...
</tr>
<tr>
<td></td>
<td>your_data</td>
...
</tr>
...
</tbody>
</table>
您的XPath失败。这就是为什么您必须在表达式中使用//
或声明tbody
元素的原因:
=IMPORTXML("https://discgolfmetrix.com/?u=scorecard&ID=1172639&view=result","//table[@class='data data-hover']/tbody/tr/td[2]")