一般的html代码都会有img,title,time等标签,要想从里面获得自己想要的那一部
分,Jsoup自然是一个很好的工具,可以在html代码中截到自己想要的东西,获得图片
地址等。
这里说的html都是普遍的,如果标签内容改变,位置改变那里面的选取角色也就变了。
public class JsoupUtils {
public static List<String> getImgs(Document doc)
{
List<String> imgs=new ArrayList<>();
Elements links = doc.select("img[src]");
for (Element element : links) {
String url=element.attr("src");
imgs.add(url);
}
return imgs;
}
public static String getTitleText(Document doc)
{
String title=null;
Elements links = doc.select("body");
for (Element element : links) {
title= element.getElementsByClass("title").text();
}
return title;
}
public static String getTime(Document doc)
{
String time=null;
Elements links = doc.select("body");
for (Element element : links) {
time= element.getElementsByClass("time").text();
}
return time;
}
}
需要传Document对象。
Document doc = Jsoup.parse(String html);
这样就搞定了、