Javascript 将cheerio对象转换为字符串

Javascript 将cheerio对象转换为字符串,javascript,html,parsing,coffeescript,cheerio,Javascript,Html,Parsing,Coffeescript,Cheerio,我正在从页面中提取元标记: $ = cheerio.load(html) metaTags = $('meta') 它工作得很好,但我需要元标记数组包含字符串,而不是cherrio的对象,如下所示: [“”,“”] p、 在美国,我不需要“角色”,只是stackoverflow.com理解错误 我提出了这样的方法: toHtml = (el) -> return el.html() 但它不起作用:获取空结果(使用map) metaTags.map(i,el)-> console.l

我正在从页面中提取元标记:

$ = cheerio.load(html)
metaTags = $('meta')
它工作得很好,但我需要元标记数组包含字符串,而不是cherrio的对象,如下所示:

[“”,“”]

p、 在美国,我不需要“角色”,只是stackoverflow.com理解错误

我提出了这样的方法:

toHtml = (el) ->
  return el.html()
但它不起作用:获取空结果(使用map)

metaTags.map(i,el)-> console.log i.toHtml(el)


这里有一个解决方案:

findMetaTags = (html) ->
  $ = cheerio.load(html)

  metatagsContainer = $('<p>')
  $('meta').each ->
    metatagsContainer.append $(this).clone()

  unless _.isEmpty(metatagsContainer)
    return metatagsContainer.html()

  return
findMetaTags=(html)->
$=cheerio.load(html)
metatagsContainer=$(“”)
$('meta')。每个->
metatagsContainer.append$(this.clone())
除非是空的(metatagsContainer)
返回metatagsContainer.html()
返回
让arrayOfHTMLstrings=$('meta').toArray().map((el,index)=>el.toString());

根据

我对这里的所有答案都有疑问

myNode.html()
在我尝试的很多情况下都没有找到
html()
方法
myNode.toString()
返回了
[object object]

最后每次都有效的是在cheerio本身上使用html()方法

const cheerio=require(“cheerio”)
html(myNode)

这将在循环或其他方式中将任何节点转换为html字符串

html()。Jquery对象包含DomeElement对象。@EmrysMyrooin在python中很容易映射(str,page.find_all('div'))是的,但您没有在这里使用python:-)
var cheerio = require('cheerio')
var html = 'SOME_HTML_STRING'
var $ = cheerio.load(html)
var htmlString = $.html()