0

クロールされたhtmlファイルからこのpraserを実行しています。このパーサーは、スレッドのタイトル、ユーザーの投稿、および合計ビューを抽出することを想定しています。私はなんとかhtmlタグを取得しましたが、問題はスレッドタイトルのすべてを取得できず、一部しか取得できないことです。

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 is back to PC wa new Rig!!
  • タイトル: [EPIC] Andyson のもう 1 つの初、Platinum Modular PSU
  • 題名:
  • タイトル: 新しい CPU を購入するのに適した SLS のショップは? . . . すぐ

この問題の回避策はありますか?

ありがとう。

4

1 に答える 1

0

Jsoup を使用して Web ページを解析する場合、最初に適切な方法で Web ドキュメントを取得する必要があります。あなたのやり方が間違っているわけではありませんが、必要以上に自分を苦しめています。

DocumentWeb ページのオブジェクトを作成するには、まず

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

このドキュメントから、フォーラムのスレッド タイトルなどを選択できます。クックブックから直接引用したもう 1 つの例は、hrefリンクです。

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

必要な要素が得られない場合は、選択が正しくありません。

ここでは、ID が で始まる<tr>すべての -elements から -elementsを選択します。<tbody>threadbits_forum

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

あなたが解析しようとしているフォーラムがわからないので、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)
...
...

これが選択を正しく取得するのに役立つことを願っています!

于 2013-08-05T21:34:03.820 に答える