Search code examples
google-sheetsweb-scrapinggoogle-sheets-formulayoutube-data-api

YouTube imported data in Google Sheets (IMPORTXML fnc) has stopped working


I want to get data from YouTube via IMPORTXML function in Google Docs.

It used to work before, but now "Imported content is empty."

For example, to get likes, I use the query:

=IF(ISNA(IMPORTXML("https://www.youtube.com/watch?v=MkgR0SxmMKo","(//*[contains(@class,'like-button-renderer-like-button')])[1]"))=TRUE,0,
     IMPORTXML("https://www.youtube.com/watch?v=MkgR0SxmMKo","(//*[contains(@class,'like-button-renderer-like-button')])[1]"))

but it is not working now, the imported content is empty.

So, the question is, what query should be to get data.


Solution

  • Your code above is scraping the YouTube site, relying on undocumented features of YouTube's API.

    Instead of being in contradiction with DTOS -- e.g. paragraphs D.7 and E.6 --, I would suggest you to use the YouTube Data API's endpoint Videos.list, for obtaining the number of likes of any given video through the property statistics.likeCount.


    Here is what DTOS says about undocumented features and scraping:

    [D.7] Undocumented Services

    You must not use undocumented APIs without express permission. You must access data from YouTube API services only according to the means stipulated in the authorized documentation of that YouTube API service.

    You must not reverse engineer undocumented YouTube API services or otherwise attempt to derive the underlying source code of these API services.

    [E.6] Scraping

    You and your API Clients must not, and must not encourage, enable, or require others to, directly or indirectly, scrape YouTube Applications or Google Applications, or obtain scraped YouTube data or content. Public search engines may scrape data only in accordance with YouTube's robots.txt file or with YouTube's prior written permission.