Google sheet 导入 XML 失败

Google sheet ImportXML fails

这个有效:

=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 标签。

GoogleSheets 以自己的方式解析页面(父 >> 子结构与您浏览器中的结构不完全相同)。在您的 XPath 中使用 //tr 来规避解析错误:

=IMPORTXML("https://discgolfmetrix.com/?u=scorecard&ID=1172639&view=result","//table[@class='data data-hover']//tr/td[2]")

或使用IMPORTHMTLQUERY

=QUERY(IMPORTHTML("https://discgolfmetrix.com/?u=scorecard&ID=1172639&view=result","table",1),"select Col2 OFFSET 1")

输出:

EDIT:更多详情:

对于第一个 link,解析的 HTML 结构如下:

<table>
    <tr>    
        <td></td>
        <td>your_data</td>
        ...
    </tr>
    <tr>    
        <td></td>
        <td>your_data</td>
        ...
    </tr>
    ...
</table>

并且您的 XPath 有效。

对于第二个 link,前面有一个包含 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]")