1.java jsoup从HTML解析获取标签节点和属性的代码:
package com.javalm.tester; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; /** * 从HTML解析某个节点的属性 * @author ld * @date 2009-12-4 下午03:29:43 */ public class JsoupTester { public static void main(String[] args) { String html = "<div class=\"bookcontent\" id=\"content\">"+ "<script src='http://files.rungolf.com/Author3/1695698/29101285.txt'></div>"; Document doc = Jsoup.parse(html); Elements elms = doc.getElementsByTag("script"); for(int i=0;i<elms.size();i++){ Element e = elms.get(i); System.out.println(e.attr("src")); } } }
2.方法说明
Elements elms = doc.getElementsByTag("script");//按标签获取节点
System.out.println(e.attr("src"));//获取节点的属性