Google工作表ImportXML失败

时间:2020-07-13 12:03:09

标签: xpath google-sheets google-sheets-formula google-sheets-importxml

此作品有效:

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

1 个答案:

答案 0 :(得分:1)

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")

输出: Golf

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]")