2012-10-22 2 views
0

포럼에서 사용자의보기 및 응답과 헤드 제목 만 추출하려고합니다. 이 코드에서 url을 제공하면 코드는 모든 것을 반환합니다. 나는 단지 title 태그에 정의 된 스레드 제목과 div content 태그 사이에있는 사용자 응답 만 원할뿐입니다. 추출 방법 도와주세요. 이 파일을 txt 파일로 인쇄하는 방법을 설명하십시오.포럼에서 스레드 헤드 및 스레드 답장을 추출하십시오.

package extract; 

import java.io.*; 

import org.jsoup.*; 

import org.jsoup.nodes.*; 

public class TestJsoup 
{ 
    public void SimpleParse() 
    {   
     try 
     { 

      Document doc = Jsoup.connect("url").get(); 

      doc.body().wrap("<div></div>"); 

      doc.body().wrap("<pre></pre>"); 
      String text = doc.text(); 
      // Converting nbsp entities 

      text = text.replaceAll("\u00A0", " "); 

      System.out.print(text); 

     } 
     catch (IOException e) 
     { 

      e.printStackTrace(); 

     } 

    } 

    public static void main(String args[]) 
    { 

     TestJsoup tjs = new TestJsoup(); 

     tjs.SimpleParse(); 

    } 

} 

답변

1

왜 div와 pre 태그에 body-element를 감쌌습니까?

제목 요소는 다음과 같이 선택할 수 있습니다

Document doc = Jsoup.connect("url").get(); 

Element titleElement = doc.select("title").first(); 
String titleText = titleElement.text(); 

// Or shorter ... 

String titleText = doc.select("title").first().text(); 

사업부 - 태그 : 여기

// Document 'doc' as above 

Elements divTags = doc.select("div"); 


for(Element element : divTags) 
{ 
    // Do something there ... eg. print each element 
    System.out.println(element); 

    // Or get the Text of it 
    String text = element.text(); 
} 

전체 Jsoup Selector API에 대한 개요이다, 이것은 당신이 어떤을 찾는 데 도움이됩니다 필요한 요소의 종류.

+0

감사 : D Buddy – Murali

+0

이 방법으로 문제가 해결 되었습니까? 아니면 div 이상 선택 했습니까? – ollo

1

음 다른 코드를 사용했고이 특정 태그에서 데이터를 수집했습니다.

요소 내용 = doc.getElementsByTag ("blockquote");

요소 k = doc.select ("[postcontent restore]");

content.select ("blockquote"). remove();

content.select ("br"). remove();

content.select ("div"). remove();

content.select ("a"). remove();

content.select ("b"). remove();

+0

하지만이 줄을 얻는 데는 어떤 문제가 있으며 이걸 제거하는 법을 모르겠습니다. Murali