这个可以用:
=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]")如果它是另一种方式,我可以理解它,因为第一个有两个tbody标签。
发布于 2020-07-13 23:38:03
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就能工作了。
对于第二个链接,前面有一个包含tr元素的tbody元素。它的结构是:
<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]")https://stackoverflow.com/questions/62875444
复制相似问题