http://lab.arc90.com/experiments/readability/是一个非常方便的工具,可以以非常易读的方式查看杂乱的报纸、期刊和博客页面。它通过使用一些启发式方法并找到网页的相关正文来做到这一点。它的源代码也可以在http://lab.arc90.com/experiments/readability/js/readability.js获得
我的一些同事引起了我的注意,因为我正在努力使用 jQuery 来获取任何报纸的“正文” | 期刊 | 博客 | 等网站。我当前的启发式(和 jQuery 中的实现)使用类似的东西(这是在 Firefox Jetpack 包中完成的):
$(doc).find("div > p").each(function (index) {
var textStr = $(this).text();
/*
We need the pieces of text that are long and in natural language,
and not some JS code snippets
*/
if(textStr.length > MIN_TEXT_LENGTH && textStr.indexOf("<script") <= 0) {
console.log(index);
console.log(textStr.length);
console.log(textStr);
$(this).attr("id", "clozefox_paragraph_" + index);
results.push(index);
wholeText = wholeText + " " + textStr;
}
});
所以它就像“去抓取 DIV 中的段落并检查不相关的字符串,如'script'”。我已经尝试过了,大多数时候它可以抓取网络文章的正文,但是我想要一个更好的启发式或者更好的 jQuery 选择机制(甚至更短?)。
你有更好的建议吗?
PS:也许“找到最里面的DIV(即没有任何DIV类型的子元素)并去抓住他们的
s only”对于我目前的目的来说是一个更好的启发式方法,但我不知道如何在 jQuery 中表达这一点。