我在用cheerio,我怎么才能得到content
我的代码没问题:
request('https://example.com', function (error, response, html) {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
console.log(html);
}
});我需要从사이드 content**:**那里得到
<meta property="og:description" content=" I'm Jack">
<meta property="og:title" content="사이드"> // How to Get `사이드` and print in console.log?发布于 2017-09-16 23:52:10
request('https://example.com', function (error, response, html) {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
console.log($("meta[property='og:title']").attr("content"));
}
});边节点(与答案中的先前错误相关):
在使用cheerio时,您应该注意到,它只模拟jQuery api的某些方面,而不重新创建整个DOM。这意味着与以前版本的答案相反,不能这样做:
$("meta").get(1).getAttribute("origin");,这将导致一个试图调用undefined的TypeError。Cheerio构建了DOM的表示,其中实现了jQuery api的一个子集。get api存在,但将返回此表示,而不是标准的DOM,并且没有附加到cheerio表示的getAttribute方法。如果您想要一个完整的DOM表示和jQuery,您需要使用类似于jsdom之类的东西。
发布于 2017-09-18 05:55:28
First way: console.log($("meta[property='og:title']").attr("content"));由@adz5A解决
秘密: console.log($("meta").eq(1).attr("content"));
.get()返回一个DOM对象。
Cheerio不是浏览器,因此DOM api不可用。
https://stackoverflow.com/questions/46257852
复制相似问题