Google Sheets — ImportXML function. Receiving Error?

与世无争的帅哥 提交于 2020-07-31 04:23:27

问题


I'm trying to use the IMPORTXML function on Google Sheets.

For example:

=IMPORTXML("https://www.tiktok.com/@charlidamelio?lang=en", XMLPATH) 

should return "72.6M"

I used the Chrome inspector to copy the xpath, which gives me:

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

When I try this in Google Sheets it returns an error: #N/A (Import Content is Empty).

P.S. I'm open to other ways to get the data I need into the google sheet, it doesn't have to use the IMPORTXML function.

I asked this question a while ago and someone gave me the following solution:

=REGEXEXTRACT(IMPORTXML(C2,"//script[@id='__NEXT_DATA__']"),"followerCount"":(\d+)")

This worked great for a while but has since stopped working.


回答1:


Issue and workaround:

I checked my proposal again. By this, I could understand that the HTML data had been changed after 2020-07-21, because when I posted this at 2020-07-21, I could confirm the xpath worked. By this change, now, I confirmed that my this proposal cannot be used anymore.

So in order to retrieve the value you expect, as the current workaround, I would like to propose to use Google Apps Script. In the current stage, it seems that when Google Apps Script is used, the value can be retrieved.

Usage:

  1. Open the script editor of the Google Spreadsheet.

  2. Copy and paste the following script to the script editor and save the script.

     function SAMPLE() {
       var url = "https://www.tiktok.com/@charlidamelio?lang=en";
       return UrlFetchApp
         .fetch(url)
         .getContentText()
         .match(/<meta name\="description"[\s\S\w]+?>/)[0]
         .match(/([.\w]+?) Fans/)[1];
     }
    
  3. Back to Google Spreadsheet, put =SAMPLE() to a cell.

    • This function is used as the custom function.

By this flow, the value is retrieved.

Note:

  • I'm not sure about how long this can be used. When the HTML structure was changed, this workaround might not be able to be used. Please be careful this.
  • When you use this script for other URL, this workaround might not be able to be used. Please be also careful this.

References:

  • Custom Functions in Google Sheets
  • Class UrlFetchApp


来源:https://stackoverflow.com/questions/63007134/google-sheets-importxml-function-receiving-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!