Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select the meta tags and get the content value in Cheerio?

I would like to select the meta tag with the og:title property and get the content text value from it i.e. Silver Surfer, I cannot figure how to approach this in Cheerio

<meta property="og:type" content="SILVER" />
<meta property="og:image" content="http://img.silversurfer.com/surfer.png"/>
<meta property="og:title" content="Silver Surfer" />
<meta property="og:url" content="https://www.silversurfer.com" />

This is my latest attempt so far var title = $('meta[property=og:title]').content

like image 320
Scene Avatar asked Oct 15 '25 05:10

Scene


1 Answers

You need to use attribute css selector and to get the attribute use Cheerio::attr.

const cheerio = require('cheerio')
const $ = cheerio.load(`
<meta property="og:type" content="SILVER" />
<meta property="og:image" content="http://img.silversurfer.com/surfer.png"/>
<meta property="og:title" content="Silver Surfer" />
<meta property="og:url" content="https://www.silversurfer.com" />
`);

$('[property="og:type"]').attr('content');
like image 61
jcubic Avatar answered Oct 16 '25 21:10

jcubic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!