gpt4 book ai didi

javascript - 如何从 Node.js IMAP 模块中的正文获取纯文本

转载 作者:搜寻专家 更新时间:2023-11-01 00:23:47 25 4
gpt4 key购买 nike

我正在使用 Node.js 的 IMAP 模块来解析 IMAP 电子邮件的正文。我可以将正文作为原始 HTML 数据返回给我,但这包括标签和其他不必要的数据。我想要输入的文本(删除任何 div、样式等)

这是我目前使用的代码:

openInbox(function(err, box) {
if (err) throw err;
var f = imap.seq.fetch(box.messages.total + ':*', { bodies: ['HEADER.FIELDS (FROM)','TEXT'] });
f.on('message', function(msg, seqno) {
console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
if (info.which === 'TEXT')
console.log(prefix + '\n\nBody [%s] found, %d total bytes\n\n\n', inspect(info.which), info.size);
var buffer = '', count = 0;
stream.on('data', function(chunk) {
count += chunk.length;
buffer += chunk.toString('utf8');
if (info.which === 'TEXT')
console.log(prefix + 'Body [%s] (%d/%d)', inspect(info.which), count, info.size);
});
stream.once('end', function() {
if (info.which !== 'TEXT')
console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer)));
else
console.log(prefix + 'Body [%s] Finished', inspect(info.which));
console.log('\n\n\n\n'+buffer.toString()+'\n\n\n\n\n\n');
});
});
msg.once('attributes', function(attrs) {
console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
});
msg.once('end', function() {
console.log(prefix + 'Finished');
});
});
f.once('error', function(err) {
console.log('Fetch error: ' + err);
});
f.once('end', function() {
console.log('Done fetching all messages!');
imap.end();
});
});

有没有一种方法可以解析为没有任何标签或其他 HTML 信息的纯文本?

最佳答案

有一个 Node 模块是为此设计的: https://www.npmjs.com/package/html-to-text

var htmlToText = require('html-to-text');

var text = htmlToText.fromString('<h1>Hello World</h1>', {
wordwrap: 130
});
console.log(text);

它还能很好地将表格解析为文本。

关于javascript - 如何从 Node.js IMAP 模块中的正文获取纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31122350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com