Reid
Reid

Reputation: 97

How can I get the value of a <script> tag in Google Sheets?

I'm trying to pull the exact number of followers for an account on Threads and have it display in a cell in a Google Sheet.

I can see in the rendered source code that there's a JSON element called follower_count that pulls in the exact number that I'm trying to get it. But I'm not able to target it exactly with the importxml function.

For example, if you look at https://threads.net/@mosseri and search the source, you'll see the follower_count with the exact number after it. If it copy the XPath of that script tag, it'll give me something (example: /html/body/script[24]) that will pull in the entire value of the script tag, but it just doesn't reliably pull in the right script tag.

Is there a way I can get that number into a Google Sheet? I've been able to target the description of the page to get a rounded number, but I would love to get that exact value.

Upvotes: 0

Views: 111

Answers (1)

rockinfreakshow
rockinfreakshow

Reputation: 29957

You may try:

=regexextract(importxml(A1,"//script[@type='application/ld+json']"),"(\d+)""}],")

Updated formula (removed comma, at the end now)

=regexextract(importxml(A2,"//script[@type='application/ld+json']"),"(\d+)""}]")

Upvotes: 1

Related Questions