2013-08-05 74 views
0

我從抓取的html文件中獲取主題。這個解析器假設提取出主題標題,用戶帖子和總體視圖。我設法得到的HTML標籤,但問題是,它無法檢索所有的線程標題,而只是得到一些。Jsoup:從論壇獲取主題標題

HTML代碼(對不起,我可憐的對齊我從網站源代碼複製):

<tbody id="threadbits_forum_2"> 

<tr> 
<td class="alt1" id="td_threadstatusicon_3396832"> 

    <img src="http://www.hardwarezone.com.sg/img/forums/hwz/statusicon/thread_hot.gif" id="thread_statusicon_3396832" alt="" border="" /> 
</td> 

    <td class="alt2">&nbsp;</td> 


<td class="alt1" id="td_threadtitle_3396832" title="Updated on 3 October 2011 

Please check Price Guides for latest prices 

A PC Buyer&#8217;s Guide that is everything to everyone is simply not possible. This  is a simple guide to putting together a PC with a local flavour. Be sure to read PC Buyer&#8217;s Guide from other media. 

If you have any..."> 


    <div> 

      <span style="float:right"> 






       <img class="inlineimg" src="http://www.hardwarezone.com.sg/img/forums/hwz/misc/sticky.gif" alt="Sticky Thread" /> 
      </span> 



     <font color=red><b>Sticky: </b></font> 


     <a href="showthread.php?s=2a7d1dc5bbc6bf85468a79ec2e6eb86e&amp;t=3396832" id="thread_title_3396832">Buyer's Guide II: Extreme, High-End, Mid-Range, Budget, and Entry Level Systems - Part 2</a> 
     <span class="smallfont" style="white-space:nowrap">(<img class="inlineimg" src="http://www.hardwarezone.com.sg/img/forums/hwz/misc/multipage.gif" alt="Multi-page thread" border="0" /> <a href="showthread.php?s=2a7d1dc5bbc6bf85468a79ec2e6eb86e&amp;t=3396832">1</a> <a href="showthread.php?s=2a7d1dc5bbc6bf85468a79ec2e6eb86e&amp;t=3396832&amp;page=2">2</a> <a href="showthread.php?s=2a7d1dc5bbc6bf85468a79ec2e6eb86e&amp;t=3396832&amp;page=3">3</a> <a href="showthread.php?s=2a7d1dc5bbc6bf85468a79ec2e6eb86e&amp;t=3396832&amp;page=4">4</a> <a href="showthread.php?s=2a7d1dc5bbc6bf85468a79ec2e6eb86e&amp;t=3396832&amp;page=5">5</a> ... <a href="showthread.php?s=2a7d1dc5bbc6bf85468a79ec2e6eb86e&amp;t=3396832&amp;page=17">Last Page</a>)</span> 
    </div> 



    <div class="smallfont"> 


      <span style="cursor:pointer" onclick="window.open('member.php?s=2a7d1dc5bbc6bf85468a79ec2e6eb86e&amp;u=39963', '_self')">adrianlee</span> 

    </div> 

我的編碼迄今:

try(BufferedReader br = new BufferedReader(new FileReader(pageThread))) 
    { 
     String html = ""; 

     while(br.readLine() != null) 
     { 
      html += br.readLine() + "\n"; 
     } 

     Document doc = Jsoup.parse(html); 
     //To get the thread list 

     Elements threadsList = doc.select("tbody[id^=threadbits_forum]").select("tr"); 

     for(Element e: threadsList) 
     { 
      //To get the title 
      System.out.println("Title: " + e.select("a[id^=thread_title]").text()); 
     } 

     System.exit(0); 

    }catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 

結果: 標題:

  • 標題:想成爲HardwareZone編輯團隊的一員?
  • 標題:
  • 標題:pa9797回到PC新的鑽機!
  • 標題:EPIC另一個先爲Andyson,白金模塊化PSU
  • 標題:
  • 標題:哪個店在SLS好購買一個新的CPU? 。 。 。 等

您是否有解決此問題的方法?

謝謝。

+0

請提供一個鏈接,你要分析的網站! –

回答

0

當使用Jsoup解析網頁時,您應該首先以正確的方式獲取網頁文檔。不是你的方式是錯誤的,但是你爲自己做的比自己更難。

要創建網頁的Document對象,具有

String url = "www.google.com"; 
Document doc = Jsoup.connect(url).get(); 

開始的從這個文件,你可以進行選擇,如論壇的主題標題。 另一個直接來自食譜的例子是href鏈接。

Elements links = doc.select("a[href]"); //a with href 

如果你沒有得到你想要的元素,那麼你的選擇是不正確的。

在這裏,您可以選擇<tr> - 從threadbits_forum開始的所有<tbody>元素的元素。

Elements threadsList = doc.select("tbody[id^=threadbits_forum]").select("tr"); 

因爲我不知道你想什麼解析論壇做,我只能看着其他threadbits的論壇,可能有他們的HTML類似的佈局。

如果你看看這個網站,http://forums.hardwarezone.com.sg/corbell-ecustomer-service-center-166/,你可以看到所有的主題都在一個<td>-類別內,名爲alt1

如果您只選擇這一項,您將獲得使用同一班級的用戶名和其他內容,但由於您只需要線程標題,因此還必須選擇<a> -tag。

這與以下選擇查詢

Elements titles = doc.select("td.alt1 a[id^=thread_title]"); 

在這個論壇上模擬你原來的問題結束了,你可以做這樣的:

String html = "http://forums.hardwarezone.com.sg/corbell-ecustomer-service-center-166/"; 
    Document doc = Jsoup.connect(html).get(); 
    Elements titles = doc.select("td.alt1 a[id^=thread_title]"); 
    for (Element e : titles) { 
     System.out.println(e.text()); 
    } 

這將導致標題:

Corbell Product Warranty Policy 
=MSI TwinFrozr user OC database= 
Notification : Change in MSI notebook service center 
RE : Forums contact window and sales & RMA reserved items @ service center : 
Corbell office location. (Thanks to tayts1) 
[Corbell] 'Like' Our Facebook Page and Get A chance To Win Attractive Prizes 
MSI R7970 lightning problem (1 fan not spinning) 
... 
... 

希望這將幫助你得到正確的選擇!

+0

非常感謝你的回覆。我去爬了論壇頁面,並保存我的本地驅動器上的HTML文件。 – user2584537

+0

你應該仍然可以使用我發佈的上述方法。 –