使用jsoup获取表的内容

时间:2015-10-09 12:50:32

标签: java android jsoup

尝试使用jsoup将URL(table: - contents)的内容抓取到arraylist中。到目前为止,在网上搜索类似于我的问题时,已经达到了死胡同。也许新鲜的眼睛会有所帮助。这是我到目前为止所做的并不多。我在某处找到了我需要识别表id然后使用Elements来遍历每一行的标签,如果是真的如何?

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();   
} 

这是我的URL html网站的输出源(相关部分): -

<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>

我想要的输出应该看起来像这个代理:50.76.49.97 port:4444国家:美国类型:透明.....

任何帮助?

1 个答案:

答案 0 :(得分:2)

    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();
        }

    }
相关问题