2012-09-07 203 views
0

我在java中使用jericho html解析器。我想從網站獲取數據。在網站的HTML內容是這樣的....如何使用jericho html解析器從網站獲取數據?

<div class="class_div"> 
    <div class="class_div2">All contents...</div>` 
    <span class="equals">Content 1</span> 
    <span class="equals">Content 2</span> 
    <span class="equals">Content 3</span> 
    <span class="equals">Content 4</span> 
</div> 

我想獲取內容1,內容2,內容3,內容4.如何獲取呢?

我使用此代碼

String sourceUrlString="<website url>"; 
if (sourceUrlString.indexOf(':')==-1) 
sourceUrlString="http:"+sourceUrlString; 
Source source=new Source(new URL(sourceUrlString)); 
Element bodyContent = source.getElementByClass("equals");` 
+0

你有什麼嘗試?顯示一些代碼,我們可以幫助指導你。如果您不知道從哪裏開始http://jericho.htmlparser.net/docs/javadoc/net/htmlparser/jericho/Tag.html,請點擊此處 –

回答

0

在哪裏的問題?有了您的代碼獲取每個Element - 與你自己的文字:

Source source = new Source(/* ... */); 
List<Element> elements = source.getAllElementsByClass("equals"); 

for(Element element : elements) 
{ 
    /* 
    * 'element.getTextExcrator().toString()' returns the text of the element 
    */ 
    System.out.println(element.getTextExtractor().toString()); 
} 

輸出:

內容1
內容2
內容3
內容4

相關問題