Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

102
Vistas
Converting mailparser HTML into DOM elements?

With mailparser when I get a lot of HTML codes how can I convert them into DOM Elements? I want to access specific HTML elements.

This is my code:

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;
                    }
                });
            });
        });
    });
});

I tried with cheerio but had no luck. How can I get access to each HTML element?

Thanks.

7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

$('span')[0] isn't a cheerio object. You can try $('span').first().text() instead.

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos