【问题标题】:Google sheet ImportXML fails谷歌表 ImportXML 失败
【发布时间】:2020-07-15 06:21:26
【问题描述】:

这个有效:

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

【问题讨论】:

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


    【解决方案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")
    

    输出:

    EDIT:更多详情:

    对于第一个链接,解析后的 H​​TML 结构如下:

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

    【讨论】:

    • 您如何确定 GoogleSheets 是如何解析页面的?还是只是经验? :)
    • 经验、代码阅读和疯狂猜测。 :) 我在帖子中添加了一些细节。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多