0

本文を抽出してコメントを除外する方法しか知りませんが、アーカイブと他の Web ページへのリンクを除外することはできません。

これは私のコードです:

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.
    }

}
4

1 に答える 1

2

以下に例を示しますが、まだ完全ではありません。不要なものをすべて削除するには、フィルタリングを追加する必要があります。

Document doc = Jsoup.connect("http://www.papagomo.com").get();


for( Element element : doc.select("div") ) // Select only 'div' tags
{
    final String ownText = element.ownText(); // Own text of this element

    if( ownText.isEmpty() )
    {
        continue; // Skip empty tags
    }
    else
    {
        System.out.println(ownText); // Output to see the result
    }
}
于 2013-05-30T12:43:44.480 に答える