【问题标题】:How to figure out proper xpath for IMPORTXML in Google Sheets - N/A Error?如何在 Google 表格中为 IMPORTXML 找出正确的 xpath - N/A 错误?
【发布时间】:2020-05-09 06:19:59
【问题描述】:

我正在尝试在 Google 表格上使用 IMPORTXML 功能。

例如:=IMPORTXML("https://www.tiktok.com/@charlidamelio?lang=en", XMLPATH) 应该返回“54.3M”

我使用 Chrome 检查器复制 xpath,这给了我:

/html/body/div[1]/div/div[2]/div/div[1]/div/header/h2[1]/strong[2]

当我在 Google 表格中尝试此操作时,它返回错误:#N/A(导入内容为空)。

附:我愿意通过其他方式将我需要的数据放入谷歌表格,它不必使用 IMPORTXML 函数。

【问题讨论】:

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


    【解决方案1】:

    这个答案怎么样?

    在此答案中,使用了 IMPORTXMLREGEXEXTRACT。而且,它假设https://www.tiktok.com/@charlidamelio?lang=en的URL被放在单元格“A1”中。

    模式一:

    在此模式中,检索“followerCount”。

    示例公式:

    =REGEXEXTRACT(IMPORTXML(A1,"//script[@id='__NEXT_DATA__']"),"followerCount"":(\d+)")
    
    • “followerCount”是从脚本中检索到的。
    • 在这种情况下,当使用=VALUE(REGEXEXTRACT(IMPORTXML(A1,"//script[@id='__NEXT_DATA__']"),"followerCount"":(\d+)"))时,检索到的值可以作为数字。

    结果:

    模式 2:

    在此模式中,检索“followerCount”。

    示例公式:

    =REGEXEXTRACT(IMPORTXML(A1,"//meta[@name='description']/@content")," ([\w\d.]+) Fans")
    
    • 从元数据中检索到“54.4M Fans”的值。

    结果:

    参考资料:

    【讨论】:

    • 非常感谢!这正是我所需要的。
    • @ConfuciusCat 感谢您的回复。很高兴您的问题得到解决。
    • 嗨@Tanaike,我非常感谢您之前的帮助,您的解决方案工作了一个多月,但现在它不再工作了。你知道为什么会这样吗? =REGEXEXTRACT(IMPORTXML(A1,"//script[@id='NEXT_DATA']"),"followerCount"":(\d+)")
    • @ConfuciusCat 感谢您的回复。现在我注意到your previous question 与此有关。所以我想评论your previous question。请检查一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多