2016-09-25 12 views
0

から、このようなものを抽出します:静的で も「testjob56」ではないOFCますがJsoupは、どのように私はこのようなサイトのHTMLからこれを取り出すことができすべての可能な方法がある場合、私は思っていたのhtml

  </tr> 
     </table> 

     <p>&nbsp;</p> 

     <table style="margin: auto"> 
      <tr> 
       <th colspan="2" style="text-align: center">Used nicknames</th> 
      </tr> 
      <tr> 
       <th>Seen on</th> 
       <th>Nickname</th> 
      </tr> 
          <tr> 
       <td>2016-09-20 04:52:21</td> 
       <td style="max-width: 400px">colored immunity man</td> 
      </tr> 
          <tr> 
       <td>2012-05-02 16:24:49</td> 
       <td style="max-width: 400px">testjob56</td> 
      </tr> 
         </table> 
       <!-- /main --> 
    </div> 

enter image description here

答えて

0

これを行う1つの方法は次のとおりです。このスニペットは、スタイル要素を持つすべてのTDを検索します。これに続いて、一度に1つの要素を取得し、たとえばスタイル(max-width)などのフィルタを使用する必要があります。セレクタを使用できるはずですが、DOMに依存します。その情報を見つけることができますhere

import java.io.IOException; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.parser.Parser; 
import org.jsoup.select.Elements; 

public class JSoupMain { 

    public static void main(String[] args) throws IOException { 

     String html = "<table style=\"margin: auto\">   <tr>    <th colspan=\"2\" style=\"text-align: center\">Used nicknames</th>   </tr>   <tr>    <th>Seen on</th>    <th>Nickname</th>   </tr>       <tr>    <td>2016-09-20 04:52:21</td>    <td style=\"max-width: 400px\">colored immunity man</td>   </tr>       <tr>    <td>2012-05-02 16:24:49</td>    <td style=\"max-width: 400px\">testjob56</td>   </tr>      </table>"; 

     Document doc = Jsoup.parse(html, "", Parser.xmlParser()); 
     /* 
     * Document doc = (Document) Jsoup .connect(
     * "https://www.google.com/search?hl=en&gl=us&tbm=nws&authuser=0&q=" + 
     * "technology") .ignoreContentType(true) .userAgent(
     * "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0" 
     *).get(); 
     */ 

     // Element link = doc.select("").first(); 
     // Elements td = doc.select("[style*='max-width: 400px']"); 
     Elements tds = doc.select("td[style]"); 
     for (Element td : tds) { 
      System.out.println(td); 
     } 

    } 

} 
関連する問題