How to scrape meta tag content using Puppeteer in Node.js

I’m working with Puppeteer for web scraping and need help extracting meta tag information. I can successfully grab page titles and paragraph text, but I’m struggling to get the content from meta tags.

Specifically, I want to extract the content attribute from meta tags like this one:
<meta name="description" content="Some website description here">

Here’s my current working code that gets titles and text successfully:

const puppeteer = require('puppeteer');

async function scrapeData() {
  const browser = await puppeteer.launch({headless: true});
  const webpage = await browser.newPage();
  await webpage.goto('https://example.com/', {waitUntil: 'domcontentloaded'});

  const siteTitle = await webpage.evaluate(() => document.querySelector('title').textContent);
  const alternateTitle = await webpage.title();
  const paragraphContent = await webpage.evaluate(() => document.querySelector('p').innerText);
  
  console.log(siteTitle);
  console.log(alternateTitle);
  console.log(paragraphContent);
}

scrapeData();

What’s the correct way to target and extract the content from meta description tags? Any help would be appreciated.

you can grab meta content with await webpage.evaluate(() => document.querySelector('meta[name="description"]').getAttribute('content')) - just add this after ur existing code and ur good to go. just make sure that meta tag exists first or ur gonna get an error.

Building on what others said - I hit the same problem scraping multiple meta tags. Way more efficient to grab them all in one evaluate call instead of separate DOM queries. Here’s what works for me:

const metaData = await webpage.evaluate(() => {
  const description = document.querySelector('meta[name="description"]')?.getAttribute('content');
  const keywords = document.querySelector('meta[name="keywords"]')?.getAttribute('content');
  const ogTitle = document.querySelector('meta[property="og:title"]')?.getAttribute('content');
  return { description, keywords, ogTitle };
});

This cuts down context switches between Node and browser, which speeds things up when you’re scraping tons of pages. Just watch out - some sites use property instead of name for certain meta tags.

You need getAttribute('content') instead of textContent for meta tags since the content lives in an attribute, not as text. Here’s what to add to your code:

const metaDescription = await webpage.evaluate(() => {
  const metaTag = document.querySelector('meta[name="description"]');
  return metaTag ? metaTag.getAttribute('content') : null;
});

The null check prevents errors when the meta tag doesn’t exist. You can grab other meta tags by switching the selector - meta[property="og:title"] for Open Graph titles or meta[name="keywords"] for keywords. Works for any meta tag attribute.