jsoup设置属性

/ / jsoup设置属性

下面的示例将HTML解析为Document对象后,使用addClass或removeClass方法来增加或删除class类方法。

Document document=Jsoup.parse(html);
Element link=document.select("a").first();         
link.attr("href","www.yahoo.com");     
link.addClass("header"); 
link.removeClass("header");    

元素对象代表dom元素,并提供了各种获取dom元素属性的方法。

addClass/removeClass示例

使用您选择的任何编辑器在C:/> jsoup中创建以下Java程序。

JsoupTester.java

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupTester {
   public static void main(String[] args) {
   
      String html = "<html><head><title>Sample Title</title></head>"
         + "<body>"
         + "<p>Sample Content</p>"
         + "<div id='sampleDiv'><a id='googleA' href='www.google.com'>Google</a></div>"
         + "<div class='comments'><a href='www.sample1.com'>Sample1</a>"
         + "<a href='www.sample2.com'>Sample2</a>"
         + "<a href='www.sample3.com'>Sample3</a><div>"
         +"</div>"
         + "<div id='imageDiv' class='header'><img name='google' src='google.png'/>"
         + "<img name='yahoo' src='yahoo.jpg'/>"
         +"</div>"
         +"</body></html>";
      Document document = Jsoup.parse(html);

      //示例:设置属性
      Element link = document.getElementById("googleA");
      System.out.println("Outer HTML Before Modification :"  + link.outerHtml());
      link.attr("href","www.yahoo.com");      
      System.out.println("Outer HTML After Modification :"  + link.outerHtml());
      System.out.println("---");
      
      //示例:addClass
      Element div = document.getElementById("sampleDiv");
      System.out.println("Outer HTML Before Modification :"  + div.outerHtml());
      link.addClass("header");      
      System.out.println("Outer HTML After Modification :"  + div.outerHtml());
      System.out.println("---");
      
      //示例:removeClass
      Element div1 = document.getElementById("imageDiv");
      System.out.println("Outer HTML Before Modification :"  + div1.outerHtml());
      div1.removeClass("header");      
      System.out.println("Outer HTML After Modification :"  + div1.outerHtml());
      System.out.println("---");
      
      //示例:批量更新
      Elements links = document.select("div.comments a");
      System.out.println("Outer HTML Before Modification :"  + links.outerHtml());
      links.attr("rel", "nofollow");
      System.out.println("Outer HTML Before Modification :"  + links.outerHtml());
   }
}

使用 javac 编译器编译类,如下所示:

无涯教程网

C:\jsoup>javac JsoupTester.java

现在运行JsoupTester以查看输出。

链接:https://www.learnfk.comhttps://www.learnfk.com/jsoup/jsoup-set-attributes.html

来源:LearnFk无涯教程网

C:\jsoup>java JsoupTester

查看输出。

Outer HTML Before Modification :<a id="googleA" href="www.google.com">Google</a>
Outer HTML After Modification :<a id="googleA" href="www.yahoo.com">Google</a>
---
Outer HTML Before Modification :<div id="sampleDiv">
 <a id="googleA" href="www.yahoo.com">Google</a>
</div>
Outer HTML After Modification :<div id="sampleDiv">
 <a id="googleA" href="www.yahoo.com" class="header">Google</a>
</div>
---
Outer HTML Before Modification :<div id="imageDiv" class="header">
 <img name="google" src="google.png">
 <img name="yahoo" src="yahoo.jpg">
</div>
Outer HTML After Modification :<div id="imageDiv" class="">
 <img name="google" src="google.png">
 <img name="yahoo" src="yahoo.jpg">
</div>
---
Outer HTML Before Modification :<a href="www.sample1.com">Sample1</a>
<a href="www.sample2.com">Sample2</a>
<a href="www.sample3.com">Sample3</a>
Outer HTML Before Modification :<a href="www.sample1.com" rel="nofollow">Sample1</a>
<a href="www.sample2.com" rel="nofollow">Sample2</a>
<a href="www.sample3.com" rel="nofollow">Sample3</a>

祝学习愉快! (发现内容有误?请选中要编辑的内容 -> 右键 -> 修改 -> 提交!帮助我们改进教程质量)

精选教程推荐

👇 以下精选教程可能对您有帮助,拓展您的技术视野

网络架构实战课 -〔谢友鹏〕

前端工程师的AI实战课 -〔柳博文〕

RAG前沿入门课 -〔老刘〕

结构思考力 · 透过结构看思考 -〔李忠秋〕

程序员的测试课 -〔郑晔〕

NLP实战高手课 -〔王然〕

iOS开发高手课 -〔戴铭〕

快速上手Kotlin开发 -〔张涛〕

React实战进阶45讲 -〔王沛〕

📝 好记忆不如烂笔头,留下您的学习笔记吧!

暂无学习笔记,成为第一个分享的人吧!

您的笔记将帮助成千上万的学习者