2012-03-31 10 views
1

URLのタイトルと説明を動的に取得する必要があります。これを行うには何を使用する必要がありますか?例えばURLを使用してタイトルと説明を動的に取得

テイク以下のURL:http://en.wikipedia.org/wiki/Stack_overflow

は私がURLのタイルと、それについての説明を抽出する必要があります。あなたは以下のようにjsoup抽出をしますか?

url.select("title"); 

はいの場合、URLの説明を抽出するにはどうすればよいですか?

+0

URLのタイトルと説明は何ですか?指定されたURLにあるHTMLページのタイトルですか?もしそうなら、その説明は何ですか?それはどこにありますか? –

+0

ええ、htmlのWebページのタイトル、メタタグからのHTMLページの説明... –

+0

あなたは解決策を見つけましたか? – jordeu

答えて

1

ジェリコのようなHTMLパーサーが必要だと思います。

は、この例を見てみましょう: http://jericho.htmlparser.net/samples/console/src/ExtractText.java

特別にこの二つの方法:

private static String getTitle(Source source) { 
    Element titleElement=source.getFirstElement(HTMLElementName.TITLE); 
    if (titleElement==null) return null; 
    // TITLE element never contains other tags so just decode it collapsing whitespace: 
    return CharacterReference.decodeCollapseWhiteSpace(titleElement.getContent()); 
} 

private static String getMetaValue(Source source, String key) { 
    for (int pos=0; pos<source.length();) { 
     StartTag startTag=source.getNextStartTag(pos,"name",key,false); 
     if (startTag==null) return null; 
     if (startTag.getName()==HTMLElementName.META) 
      return startTag.getAttributeValue("content"); // Attribute values are automatically decoded 
     pos=startTag.getEnd(); 
    } 
    return null; 
} 
関連する問題