[英]Using jsoup to get contents of a table
我正在嘗試使用jsoup將URL的內容(表:-內容)捕獲到arraylist中。 到目前為止,在網上搜索類似於我的問題時已經陷入僵局。 也許新鮮的眼睛會有所幫助。 到目前為止,這還不算什么。我讀了某個地方,我需要識別表ID,然后使用Elements遍歷每一行的標記,如果為true,該如何做?
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端口:4444國家/地區:美國類型:透明.....
有什么幫助嗎?
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.