import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
* 針對
* http://zhidao.baidu.com/question/423488719.html?fr=uc_push&push=ql&oldq=1問題
* (http://www.appannie.com/top/iphone/united-states/games/ 用jsoup抓取這個網站的信息並輸出Free一欄中排名上升度大於30的遊戲,最好有詳細代碼)
* 編寫的利用JSOUP寫的排序程序
*
*
@author ZLG
*
*/
public class TJsoup {
public List<TopFreeValue> list = new ArrayList<TopFreeValue>();
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Document document;
try {
document = Jsoup.connect("http://www.appannie.com/top/iphone/united-states/games/#")
.data("query","java").userAgent("ie").timeout(30000).cookie("auth", "token").post();
Elements ele = document.select("td.top_free*");
List<TopFreeValue> list = new ArrayList<TopFreeValue>();
for(Element element:ele){
String topvalue = element.select("span").first().text();
if(topvalue!=null && topvalue.startsWith("▲")){
String name = element.select("a").first().text();
Integer value = Integer.valueOf(topvalue.substring(1, topvalue.length()));
if(value!=null && value>30){
TJsoup th = new TJsoup();
TopFreeValue free = th.new TopFreeValue();
free.setName(name);
free.setUpvalue(value);
list.add(free);
}
}
}
Collections.sort(list);
// list.subList(0, 30);
System.out.println(list.size());
for(TopFreeValue value:list){
System.out.println(value.getName()+" .... "+value.getUpvalue());
}
} catch (IOException e) {
e.printStackTrace();
} catch(ClassCastException e1){
e1.printStackTrace();
}
}
/**
* 定義內部類 內部類實現 name--value
* 而且實現comparable接口
*
@author ZLG
*
*/
public class TopFreeValue implements Comparable<Object>{
public int Upvalue;
public String name;
/**
* @param upvalue the upvalue to set
*/
public void setUpvalue(int upvalue) {
Upvalue = upvalue;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
*
@return the upvalue
*/
public int getUpvalue() {
return Upvalue;
}
/**
*
@return the name */ public String getName() { return name; } @Override public int compareTo(Object o) { if(o instanceof TopFreeValue){ TopFreeValue free = (TopFreeValue)o; return free.Upvalue-this.Upvalue; }else{ throw new ClassCastException(); } } }; }