cheeriojs/cheerio

Cherrio is unable to retrieve content with property of og in the meta tag

luoriwusheng-xia opened this issue · 3 comments

base example

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta property="og:title" content="预览标题">
  <meta property="og:description" content="预览描述">
  <meta property="og:image:width" content="40">
  <meta property="og:image:height" content="80">
  <meta property="og:image" content="http://xxxsasd.png">
  <meta property="og:url" content="链接的URL">
</head>

<body>
  <div>test web site</div>
</body>

</html>

use live-server start a local web site

nodejs code

import { load } from 'cheerio'

const fetchTargetText = (url: string): Promise<string> => {
	return new Promise((resolve, reject) => {
		fetch(url)
			.then((r) => r.text())
			.then((res) => {
				resolve(res)
			})
			.catch((e) => {
				reject(e)
			})
	})
}

const const resolveText = (text: string) => {
    const $ = load(text)
    let t = $('meta')
    
    let aa =$('meta[property="og:"]')
    
    console.log(t);
    
  // ? The length of the content obtained here is 0
    console.log(aa);
}


const run = async() => {
  
  let text = await fetchTargetText(url)
  
  return resolveText(text)
}


run ()
fb55 commented

Try [property^="og:"]. You are currently looking for an exact match, where there are none.

Try [property^="og:"]. You are currently looking for an exact match, where there are none.

Still not working

node: v20.10.0
 "cheerio": "1.0.0-rc.12"