package com.example.gaoerfu1.golf_news;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
public class WebContent {
// public static void main(String[] args) throws IOException {
// WebContent wb = new WebContent();
// wb.getCont();
// }
public List<ContentBean> getCont() throws IOException {
String url = "http://golf.sina.cn/?vt=4";
String elem = "div.lS1>p";
try {
Document doc = Jsoup.connect(url).timeout(0).get();
Elements items = doc.select(elem);
List<ContentBean> list = new ArrayList<ContentBean>();
int i = 0;
for (Element item : items) {
i++;
ContentBean cb = new ContentBean();
Node node = item.childNode(0);
String emp = node.toString().trim();
if ("".equals(emp)) {
node = item.childNode(1);
}
// 网址
String ulr2 = node.attr("href");
cb.setUrl(ulr2);
// 标题
String name = node.toString();
name = name.substring(name.indexOf(">") + 1,
name.lastIndexOf("<"));
cb.setTitle(name);
String elem2 = "p.prot";
Document doc2 = Jsoup.connect(ulr2).timeout(0).get();
Elements items2 = doc2.select(elem2);
for (Element item1 : items2) {
// 时间
String date = item1.childNode(0).toString();
cb.setDate(date);
//来源
String src = item1.childNode(1).toString();
src = src.substring(src.indexOf(">") + 1,
src.lastIndexOf("<"));
cb.setSrc(src);
}
list.add(cb);
if (i == 4) {
break;
}
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}