【问题标题】:How to select the meta tags and get the content value in Cheerio?如何在 Cheerio 中选择元标记并获取内容值?
【发布时间】:2021-02-13 14:40:49
【问题描述】:

我想选择带有og:title 属性的元标记并从中获取内容文本值,即Silver Surfer,我不知道如何在 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" />

这是我迄今为止的最新尝试 var title = $('meta[property=og:title]').content

【问题讨论】:

  • 如果您无法弄清楚,您一定尝试了几件事。给他们看。
  • 刚刚编辑了我原来的帖子!

标签: html jquery jquery-selectors cheerio


【解决方案1】:

您需要使用属性 css 选择器并使用 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');

【讨论】:

  • 我没有正确加载内容,因此我的方法不起作用!
猜你喜欢
  • 1970-01-01
  • 2021-02-26
  • 2015-04-13
  • 2013-12-21
  • 2014-11-10
  • 2019-07-30
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
相关资源
最近更新 更多