Scrape H1 in Google Sheets

Learn how to scrape H1 tags from multiple URLs using a custom function in Google Sheets with Apps Script. This tutorial simplifies the process of extracting important header information from web pages.

So H ones are super important and maybe you're doing some research and you're trying to find the H one of a bunch of URLs, a bunch of websites, and you're like, okay, I'm gonna go to the website, I'm gonna copy the H one and I have to like look through the code a little bit. Maybe let's cut through all of that. Let's make an app script to do this for us and we're gonna put it into a custom function . So we just have to type in equals get H one, something like that, and we will get the H one. So let's go up to Extensions app script . Here, let's create our function Git, H one. And inside of our parentheses, we need a URL. That's just a text, a little variable that says grab the URL that we're gonna put in there. And that could be a reference to a cell. And if. There is no URL, then we just wanna return nothing. But if there is, we're going to try.

And why we're gonna do try is because sometimes grabbing things from the internet and doing the URL fetch, sometimes it gets an error that we wanna actually see. So we're gonna try it and catch. And we're gonna do E and then we're gonna return, sorry. Error and we can actually get e message. I think it's that. Alright, we're gonna try variable HTML equals URL fetch app. Do fetch URL. We're gonna do, get content text. And now we need to do a match . We need to say, Hey, when we get this HTML back, let's match . Now. This is a bit of a rejects or regular expressions or rejects. We're gonna get H one slash H one. Get everything up to that second close quote.

Asterisk. Basically this is Reg X that says, Hey, get that H one, but not only the H ones that have this bracket, we want even H ones that have some kind of class in it, some kind of more information. So it's just that. First bracket, then each one without the second bracket until we, maybe we have some text there until, until the second slash H one, or rather the first one that has the slash And if there is no match , which sometimes if we're using. A URL that is loaded by JavaScript. Sometimes we actually don't see that H one right away, and this is a good indication that Google might not see that H one as well. We'll do return note H one found. We will return the match . We'll replace. Any internal tags that there are, and we'll trim it so it makes, this,

makes it a little bit more clean than you might imagine right off the bat. I also wanna do one extra special thing, which is do slash asterisk asterisk at custom function at the top. Let's, let's rename this. So altogether we're going to have a bit of, hey, if there's no URL, if there's no match , we got some answers . It's not gonna just give us an error or nothing. We're going to go to the URL that we get, get that match for that H one and return it. Let's check it. Let's go get H one. It's right there. The URL is a four. There we go. So we're starting to get our H ones. We see that this has none. No. Each one found. That's interesting. And now maybe I can go individually to each one of those and see, you know, there's probably some kind of JavaScript going on.

It is great. I see actually the real return code here, 4 0 6 for entrepreneur.com. Probably some kind of JavaScript thing with this, and it's probably better for articles like we go to National Geographic. Here we have an actual article. If we go to 10 TechCrunch, there might be some actual H one for an article there. See, that's probably the better H one to get probably better examples. Even like stripe.com isn't that great with an H one that they show just global payments. But at least this helps you with a lot of copy paste. There you go. That's how you get the H one and make it a custom function inside your Google sheet. You are watching better sheets here on YouTube. Make sure you check out this video or this video and subscribe right

now to get more tips, tricks, how tos, get more out of your Google sheets than you ever have before. I'm excited to be making a ton more videos here. Ask me questions down in the comments and I will answer them in future videos. But for right now, right here, one of these videos is gonna be your next Google sheet.