2016-09-25 26 views
0

我想知道是否有任何可能的方式,我可以從這樣一個網站的HTML提取此: 也是「testjob56」將OFC不是靜態Jsoup我將如何提取像這樣從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

這樣做的一種方法如下。這段代碼用一個樣式元素搜索所有的TD。在此之後,您應該能夠一次獲得一個元素,並且例如在樣式(最大寬度)上使用過濾器。你應該能夠使用選擇器,但取決於你的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); 
     } 

    } 

}