问题
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:
Open the script editor of the Google Spreadsheet.
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]; }
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