简体   繁体   中英

Using jsoup to get contents of a table

Am trying to use jsoup to grap the contents of a URL (table:- contents) into an arraylist. So far, have hit dead ends when searching online for questions similar to mine. Maybe a fresh eyes will help. This is what I have so far which is not much.I read somewhere that I need to identify the table id then work using Elements to loop through the tag for each row, if true how?

try {               
    Document doc = Jsoup.connect("http://www.us-proxy.org").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36").get();
    //utilize the fetched html    
} catch(Exception e{  
    e.printStackTrace();   
} 

Here is my output source of the URL html site(relevant part) :-

<table cellpadding="0" cellspacing="0" border="0" class="display fpltable" id="proxylisttable">
  <thead>
    <tr>
      <th>IP Address</th>
      <th>Port</th>
      <th>Code</th>
      <th>Country</th>
      <th>Anonymity</th>
      <th>Google</th>
      <th>Https</th>
      <th>Last Checked</th>
    </tr>
  </thead>
  <tbody>
    <tr><td>24.210.34.226</td><td>3128</td><td>US</td><td>United States</td><td>transparent</td><td>no</td><td>no</td><td>18 hours 20 minutes ago</td></tr>
    <tr><td>50.76.49.97</td><td>4444</td><td>US</td><td>United States</td><td>transparent</td><td>no</td><td>no</td><td>18 hours 20 minutes ago</td></tr>
    <tr><td>
  </tbody>
  <tfoot>
    <tr>
      <th class="input"><input type="text" /></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </tfoot>
</table>

My desired output should look something like this proxy : 50.76.49.97 port:4444 country: United States type: Transparent .....

Any help?

    Elements elements = doc.select("table[class=display fpltable]");

    Elements rows = elements.get(0).select("tr");

    for (Element row : rows) {

        if (row.select("td").size() == 8) {
            String iPAddress = row.select("td").get(0).text();
            String port = row.select("td").get(1).text();
            String code = row.select("td").get(2).text();
            String country = row.select("td").get(3).text();
            String anonymity = row.select("td").get(4).text();
            String google = row.select("td").get(5).text();
            String https = row.select("td").get(6).text();
            String lastChecked = row.select("td").get(7).text();
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM