Con mailparser cuando obtengo muchos códigos HTML, ¿cómo puedo convertirlos en elementos DOM? Quiero acceder a elementos HTML específicos.
Este es mi código:
var imaps = require('imap-simple'); const _ = require('lodash'); const simpleParser = require('mailparser').simpleParser; const cheerio = require('cheerio'); var config = { imap: { user: 'xxx', password: 'xxx', host: 'xxx', port: 993, tls: true, authTimeout: 4000 } }; imaps.connect(config).then(function (connection) { return connection.openBox('INBOX').then(function () { var searchCriteria = [ 'UNSEEN' ]; var fetchOptions = { bodies: ['HEADER', 'TEXT', ''], }; return connection.search(searchCriteria, fetchOptions).then(function (messages) { messages.forEach(function (item) { var all = _.find(item.parts, { "which": "" }) var id = item.attributes.uid; var idHeader = "Imap-Id: "+id+"\r\n"; simpleParser(idHeader+all.body, (err, mail) => { if (mail.subject.indexOf('example.com') > -1) { var dom = mail.html; const $ = cheerio.load(mail.html); var testing = $('span')[0].text(); } else { //return false; } }); }); }); }); });
Probé con Cheerio pero no tuve suerte. ¿Cómo puedo obtener acceso a cada elemento HTML?
Gracias.
$('span')[0]
no es un objeto cheerio. Puede probar $('span').first().text()
en su lugar.