有什么方法可以过滤html文档的数据吗



我目前正在开发一个chrome扩展,它使用网站的html文档来提取数据,但我需要制作一个过滤器来获得我想要的内容。

在这种尝试中,扩展获取页面的HTML,并将其转换为字符串,以便轻松操作:

//This method gets a string and counts how many times
//the word you're looking for its in the string
function countWordInAString(string, word) {
return (string.match(new RegExp(word, "g")) || []).length;
}
function getOutlookData(html) {
var unreaded = countWordInAString(html, 'no leídos');
var readed = countWordInAString(html, 'leídos');
var totalMails = countWordInAString(html, 'id="AQAAA1thnTQBAAAEA7R1mgAAAAA="');
var message = totalMails + 'Mails loaded! n Mails readed: ' + readed + 'n Mails unreaded: ' + unreaded;
return message + 'n' + "HTML:n" + html;
}

它在某些特定情况下有效,但对于混淆的网站(如本例中的outlook(,结果是错误的。我能做些什么来改进它?

您的"word"可能包含特殊字符。传递给正则表达式时,使用反斜杠对其进行编码即

const encodeForReg = str => str.replace(/([^sw])/g, '\$1');
function countWordInAString(string, word) {
const encodedWord = encodeForReg(word);
return (string.match(new RegExp(encodedWord, "g")) || []).length;
}
id="AQAAA1thnTQBAAAEA7R1mgAAAAA="

成为

id="AQAAA1thnTQBAAAEA7R1mgAAAAA="

最新更新