Search code examples
google-apps-scriptgoogle-sheetsweb-scrapingxpathgoogle-sheets-formula

Google Sheet IMPORTXML "Imported content is empty"


I want to get the number from this website (https://www.shazam.com/track/488840226/cruel-summer)

But it's return "imported content is empty"

=IMPORTXML("https://www.shazam.com/track/488840226/cruel-summer","//*[@id='/track/info/488840226']/article[2]/div[2]/div[2]/div[1]/div/div/p/span/span/em")

The number on this website changes every hour, so I want to get the live number on google sheet.

How to solve this problem?


Solution

  • The problem is that the website is rendered with JavaScript, so you must use Playwright/Selenium/Puppeteer or similar, as @idchi suggested. But since you are using Google Sheet, and their Rest API is pretty lenient, I offer an alternative to call their API directly in Google Sheets.

    =REGEXEXTRACT(
        IMPORTDATA(
            CONCAT("https://www.shazam.com/services/count/v2/web/track/", REGEXEXTRACT(<URL>, "/track/(\d+)/")),  "ㅤ"
            # specify some random stuff as delimiter so Google doesn't split the data as CSV
        ), """total"":\s*(\d+)"
            # and let's extract the count!
    )