我只知道如何提取正文并排除评论,但无法排除存档和指向其他网页的链接。
这是我的代码:
package CrawlerMain;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Node;
public class MainFour {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect("http://www.papagomo.com").get();
//get text only
removeComments(doc);
String text = doc.body().text();
System.out.println(text);
}
private static void removeComments(Node node) {
int i = 0;
while (i < node.childNodes().size()) {
Node child = node.childNode(i);
if (child.nodeName().equals("#comment"))
child.remove();
else {
removeComments(child);
i++;
}
} //To change body of generated methods, choose Tools | Templates.
}
}