Jsoup案例:http://www.open-/jsoup/
操作HTML内容:
style='vertical-align:middle;'/>
代码案例:packagecom.what21.jsoup.image;
importjava.io.IOException;
importorg.jsoup.Jsoup;
importorg.jsoup.nodes.Document;
importorg.jsoup.nodes.Element;
importorg.jsoup.select.Elements;
publicclassJsoupImage{
publicstaticvoidmain(String[]args)throwsIOException{
//body
StringhtmlBody="";
//解析成文档对象
Documentdoc=Jsoup.parse(htmlBody);
//返回所有的Element
Elementseles=doc.getAllElements();
//遍历所有的文档
for(Elementele:eles){
StringtagName=ele.tagName();
if("img".equalsIgnoreCase(tagName)){
//替换class属性为what21
ele.attr("class","what21");
//替换样式为空
ele.attr("style","");
//替换成新的图片地址
ele.attr("src","//6064cb01223d3a4c.png");
}
}
//输出
StringnewsBody=doc.toString();
System.out.println(newsBody);
}
}
输出内容:
访问图片的绝对路径:element.attr("abs:src");