jsoup学习

来源:互联网 发布:linux wireshark 编辑:程序博客网 时间:2024/05/01 07:40

importjava.io.IOException;

importorg.jsoup.Jsoup;

importorg.jsoup.nodes.Document;

importorg.jsoup.nodes.Element;

 

public class jsoup {

 

publicstatic void main(String[] args) {

Stringcontent = "<p> </p>"

+"<p><img src=\"http://localhost:81//cloud/investment/0022024394_small_edit.jpg\"imgid=\"146\" style=\"float:none;\" /></p> "

+"<p><img src=\"http://localhost:81//cloud/investment/0022024395_small_edit.jpg\"imgid=\"145\" style=\"float:none;\" /></p> "

+"<img src=\"http://localhost:81//cloud/investment/0022024396_small_edit.jpg\"imgid=\"144\" style=\"float:none;\" /> "

+"<p><img src=\"http://localhost:81//cloud/investment/0022024397_small_edit.jpg\"imgid=\"147\" /></p> "

+"<p><br /></p> " + "<p></p>";

Documentdoc = Jsoup.parse(content);

//                geturl();        

//                gettext();

//                addattr(doc);

//                updatetext(doc);

//                addtag(doc);

/*        选择器:http://www.open-open.com/jsoup/selector-syntax.htm

Elementsimgs = doc.select("p:has(img[imgid="+imgId+"])");

if(imgs!= null && imgs.size() > 0){

imgs.remove();

}

for(Map.Entry<String,String> en : idMap.entrySet()){

Elementsis = imgs.select("img[imgid="+en.getKey()+"]");

}*/

}

 

publicstatic void geturl() {

//                从文件加载

//                Fileinput = new File("/tmp/input.html");

//                Documentdoc = Jsoup.parse(input, "UTF-8", "http://example.com/");

Documentdoc = null;

//                从url加载        

try{

doc= Jsoup.connect("http://www.open-open.com").get();

}catch (IOException e) {

}

Elementlink = doc.select("a").first();

StringrelHref = link.attr("href"); // == "/"

StringabsHref = link.attr("abs:href"); // "http://www.open-open.com/"

System.out.println(relHref);//<a href="/download">...</a>. 当你使用 Node.attr(String key)方法来取得a元素的href属性时,它将直接返回在HTML源码中指定定的值。

System.out.println(absHref);

}

 

publicstatic void addattr(Document doc) {

doc.select("pimg").attr("rel", "nofollow");// 增加属性div.comments

doc.select("pimg").attr("rel","nofollow").addClass("round-box");// 增加属性后返回ele,再加样式

System.out.println(doc.body().html());

}

 

publicstatic void gettext() {

Stringhtml = "<p>An <a href='http://example.com/'><b>example</b></a>link.</p>";

Documentdoc = Jsoup.parse(html);//解析HTML字符串返回一个Document实现

Elementlink = doc.select("a").first();//查找第一个a元素

 

Stringtext = doc.body().text(); // "An example link"//取得字符串中的文本

StringlinkHref = link.attr("href"); // "http://example.com/"//取得链接地址

StringlinkText = link.text(); // "example""//取得链接地址中的文本

 

StringlinkOuterH = link.outerHtml();

    // "<a href="http://example.com"><b>example</b></a>"

StringlinkInnerH = link.html(); //"<b>example</b>"//取得链接内的html内容

}

 

publicstatic void updatetext(Document doc) {

Elementdiv = doc.select("p").first(); // 修改元素文本; <, > 等这样的字符,将以文本处理

div.text("five> four");

div.prepend("First");

div.append("Last");

System.out.println(doc.body().html());

}

 

publicstatic void addtag(Document doc) {

Elementdiv = doc.select("p").first();// <p>lorem ipsum</p>

div.html("<p>loremipsum</p>");

div.prepend("<p>First</p>");

div.append("<p>Last</p>");

//添完后的结果: <div><p>First</p><p>loremipsum</p><p>Last</p></div>

System.out.println("前后tag:"+ doc.body().html());

 

Elementspan = doc.select("p").first(); // <span>One</span>

span.wrap("<li><ahref='http://example.com/'></a></li>");

//添完后的结果: <li><a href="http://example.com"><span>On

}

}

0 0
原创粉丝点击