JSOUP初探

JSOUP是偶然看到的一个处理HTML的JAVA 类库,其官方网址是:http://jsoup.org/

1、编写相关的试用程序(只需要在工程中引用jsoup-1.3.3.jar即可):

  1. import java.io.File;  
  2. import java.io.IOException;  
  3.   
  4. import org.jsoup.Jsoup;  
  5. import org.jsoup.nodes.Document;  
  6. import org.jsoup.select.Elements;  
  7.   
  8. public class Test {  
  9.     public static void main(String[] args) {  
  10.         Test t = new Test();  
  11.         t.parseFile();  
  12.     }  
  13.   
  14.     public void parseString() {  
  15.         String html = "<html><head><title>blog</title></head><body οnlοad='test()'><p>Parsed HTML into a doc.</p></body></html>";  
  16.         Document doc = Jsoup.parse(html);  
  17.         System.out.println(doc);  
  18.         Elements es = doc.body().getAllElements();  
  19.         System.out.println(es.attr("onload"));  
  20.         System.out.println(es.select("p"));  
  21.     }  
  22.   
  23.     public void parseUrl() {  
  24.         try {  
  25.             Document doc = Jsoup.connect("http://www.baidu.com/").get();  
  26.             Elements hrefs = doc.select("a[href]");  
  27.             System.out.println(hrefs);  
  28.             System.out.println("------------------");  
  29.             System.out.println(hrefs.select("[href^=http]"));  
  30.         } catch (IOException e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.     }  
  34.   
  35.     public void parseFile() {  
  36.         try {  
  37.             File input = new File("input.html");  
  38.             Document doc = Jsoup.parse(input, "UTF-8");  
  39.             // 提取出所有的编号   
  40.             Elements codes = doc.body().select("td[title^=IA] > a[href^=javascript:view]");  
  41.             System.out.println(codes);  
  42.             System.out.println("------------------");  
  43.             System.out.println(codes.html());  
  44.         } catch (IOException e) {  
  45.             e.printStackTrace();  
  46.         }  
  47.     }  
  48. }  
import java.io.File;
import java.io.IOException;

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

public class Test {
	public static void main(String[] args) {
		Test t = new Test();
		t.parseFile();
	}

	public void parseString() {
		String html = "<html><head><title>blog</title></head><body οnlοad='test()'><p>Parsed HTML into a doc.</p></body></html>";
		Document doc = Jsoup.parse(html);
		System.out.println(doc);
		Elements es = doc.body().getAllElements();
		System.out.println(es.attr("onload"));
		System.out.println(es.select("p"));
	}

	public void parseUrl() {
		try {
			Document doc = Jsoup.connect("http://www.baidu.com/").get();
			Elements hrefs = doc.select("a[href]");
			System.out.println(hrefs);
			System.out.println("------------------");
			System.out.println(hrefs.select("[href^=http]"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void parseFile() {
		try {
			File input = new File("input.html");
			Document doc = Jsoup.parse(input, "UTF-8");
			// 提取出所有的编号
			Elements codes = doc.body().select("td[title^=IA] > a[href^=javascript:view]");
			System.out.println(codes);
			System.out.println("------------------");
			System.out.println(codes.html());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


 

2、parseString的输出:

  1. <html>  
  2.  <head>  
  3.   <title>blog</title>  
  4.  </head>  
  5.  <body οnlοad="test()">  
  6.   <p>Parsed HTML into a doc.</p>  
  7.  </body>  
  8. </html>  
  9. test()  
  10.   
  11. <p>Parsed HTML into a doc.</p>  
<html>
 <head>
  <title>blog</title>
 </head>
 <body οnlοad="test()">
  <p>Parsed HTML into a doc.</p>
 </body>
</html>
test()

<p>Parsed HTML into a doc.</p>


 

3、parseUrl的输出:

  1. <a href="/gaoji/preferences.html">设置</a>  
  2. <a href="http://passport.baidu.com/?login&tpl=mn">登录</a>  
  3. <a href="http://news.baidu.com">新 闻</a>  
  4. <a href="http://tieba.baidu.com">贴 吧</a>  
  5. <a href="http://zhidao.baidu.com">知 道</a>  
  6. <a href="http://mp3.baidu.com">MP3</a>  
  7. <a href="http://image.baidu.com">图 片</a>  
  8. <a href="http://video.baidu.com">视 频</a>  
  9. <a href="http://map.baidu.com">地 图</a>  
  10.   
  11. <a href="#" name="ime_hw">手写</a>  
  12.   
  13. <a href="#" name="ime_py">拼音</a>  
  14.   
  15. <a href="#" name="ime_cl">关闭</a>  
  16. <a href="http://hi.baidu.com">空间</a>  
  17. <a href="http://baike.baidu.com">百科</a>  
  18. <a href="http://www.hao123.com">hao123</a>  
  19. <a href="/more/">更多>></a>  
  20. <a id="st" οnclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.baidu.com')" href="http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com">把百度设为主页</a>  
  21. <a href="http://e.baidu.com/?refer=888">加入百度推广</a>  
  22. <a href="http://top.baidu.com">搜索风云榜</a>  
  23. <a href="http://home.baidu.com">关于百度</a>  
  24. <a href="http://ir.baidu.com">About Baidu</a>  
  25. <a href="/duty/">使用百度前必读</a>  
  26. <a href="http://www.miibeian.gov.cn" target="_blank">京ICP证030173号</a>  
  27. ------------------  
  28. <a href="http://passport.baidu.com/?login&tpl=mn">登录</a>  
  29. <a href="http://news.baidu.com">新 闻</a>  
  30. <a href="http://tieba.baidu.com">贴 吧</a>  
  31. <a href="http://zhidao.baidu.com">知 道</a>  
  32. <a href="http://mp3.baidu.com">MP3</a>  
  33. <a href="http://image.baidu.com">图 片</a>  
  34. <a href="http://video.baidu.com">视 频</a>  
  35. <a href="http://map.baidu.com">地 图</a>  
  36. <a href="http://hi.baidu.com">空间</a>  
  37. <a href="http://baike.baidu.com">百科</a>  
  38. <a href="http://www.hao123.com">hao123</a>  
  39. <a id="st" οnclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.baidu.com')" href="http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com">把百度设为主页</a>  
  40. <a href="http://e.baidu.com/?refer=888">加入百度推广</a>  
  41. <a href="http://top.baidu.com">搜索风云榜</a>  
  42. <a href="http://home.baidu.com">关于百度</a>  
  43. <a href="http://ir.baidu.com">About Baidu</a>  
  44. <a href="http://www.miibeian.gov.cn" target="_blank">京ICP证030173号</a>  
<a href="/gaoji/preferences.html">设置</a>
<a href="http://passport.baidu.com/?login&tpl=mn">登录</a>
<a href="http://news.baidu.com">新 闻</a>
<a href="http://tieba.baidu.com">贴 吧</a>
<a href="http://zhidao.baidu.com">知 道</a>
<a href="http://mp3.baidu.com">MP3</a>
<a href="http://image.baidu.com">图 片</a>
<a href="http://video.baidu.com">视 频</a>
<a href="http://map.baidu.com">地 图</a>

<a href="#" name="ime_hw">手写</a>

<a href="#" name="ime_py">拼音</a>

<a href="#" name="ime_cl">关闭</a>
<a href="http://hi.baidu.com">空间</a>
<a href="http://baike.baidu.com">百科</a>
<a href="http://www.hao123.com">hao123</a>
<a href="/more/">更多>></a>
<a id="st" οnclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.baidu.com')" href="http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com">把百度设为主页</a>
<a href="http://e.baidu.com/?refer=888">加入百度推广</a>
<a href="http://top.baidu.com">搜索风云榜</a>
<a href="http://home.baidu.com">关于百度</a>
<a href="http://ir.baidu.com">About Baidu</a>
<a href="/duty/">使用百度前必读</a>
<a href="http://www.miibeian.gov.cn" target="_blank">京ICP证030173号</a>
------------------
<a href="http://passport.baidu.com/?login&tpl=mn">登录</a>
<a href="http://news.baidu.com">新 闻</a>
<a href="http://tieba.baidu.com">贴 吧</a>
<a href="http://zhidao.baidu.com">知 道</a>
<a href="http://mp3.baidu.com">MP3</a>
<a href="http://image.baidu.com">图 片</a>
<a href="http://video.baidu.com">视 频</a>
<a href="http://map.baidu.com">地 图</a>
<a href="http://hi.baidu.com">空间</a>
<a href="http://baike.baidu.com">百科</a>
<a href="http://www.hao123.com">hao123</a>
<a id="st" οnclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.baidu.com')" href="http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com">把百度设为主页</a>
<a href="http://e.baidu.com/?refer=888">加入百度推广</a>
<a href="http://top.baidu.com">搜索风云榜</a>
<a href="http://home.baidu.com">关于百度</a>
<a href="http://ir.baidu.com">About Baidu</a>
<a href="http://www.miibeian.gov.cn" target="_blank">京ICP证030173号</a>


 

3、parseFile的输出:

  1. <a href="javascript:view('67530','67530','0');">IA100908-002</a>  
  2.   
  3. <a href="javascript:view('67529','67529','0');">IA100908-001</a>  
  4.   
  5. <a href="javascript:view('67544','67544','0');">IA100908-016</a>  
  6.   
  7. <a href="javascript:view('67364','67364','0');">IA100903-008</a>  
  8.   
  9. <a href="javascript:view('67363','67363','0');">IA100903-007</a>  
  10.   
  11. <a href="javascript:view('66104','66104','0');">IA100710-013</a>  
  12.   
  13. <a href="javascript:view('57916','57916','0');">IA100515-013</a>  
  14.   
  15. <a href="javascript:view('56962','56962','0');">IA100430-022</a>  
  16.   
  17. <a href="javascript:view('66958','66958','0');">IA100830-001</a>  
  18.   
  19. <a href="javascript:view('66319','66319','0');">IA100713-003</a>  
  20.   
  21. <a href="javascript:view('66317','66317','0');">IA100713-001</a>  
  22.   
  23. <a href="javascript:view('66321','66321','0');">IA100713-005</a>  
  24.   
  25. <a href="javascript:view('66967','66967','0');">IA100830-010</a>  
  26.   
  27. <a href="javascript:view('66999','66999','0');">IA100831-001</a>  
  28.   
  29. <a href="javascript:view('67377','67377','0');">IA100904-004</a>  
  30.   
  31. <a href="javascript:view('67378','67378','0');">IA100904-005</a>  
  32.   
  33. <a href="javascript:view('3271','3271','0');">IA080115-031</a>  
  34. ------------------  
  35. IA100908-002  
  36. IA100908-001  
  37. IA100908-016  
  38. IA100903-008  
  39. IA100903-007  
  40. IA100710-013  
  41. IA100515-013  
  42. IA100430-022  
  43. IA100830-001  
  44. IA100713-003  
  45. IA100713-001  
  46. IA100713-005  
  47. IA100830-010  
  48. IA100831-001  
  49. IA100904-004  
  50. IA100904-005  
  51. IA080115-031  
<a href="javascript:view('67530','67530','0');">IA100908-002</a>

<a href="javascript:view('67529','67529','0');">IA100908-001</a>

<a href="javascript:view('67544','67544','0');">IA100908-016</a>

<a href="javascript:view('67364','67364','0');">IA100903-008</a>

<a href="javascript:view('67363','67363','0');">IA100903-007</a>

<a href="javascript:view('66104','66104','0');">IA100710-013</a>

<a href="javascript:view('57916','57916','0');">IA100515-013</a>

<a href="javascript:view('56962','56962','0');">IA100430-022</a>

<a href="javascript:view('66958','66958','0');">IA100830-001</a>

<a href="javascript:view('66319','66319','0');">IA100713-003</a>

<a href="javascript:view('66317','66317','0');">IA100713-001</a>

<a href="javascript:view('66321','66321','0');">IA100713-005</a>

<a href="javascript:view('66967','66967','0');">IA100830-010</a>

<a href="javascript:view('66999','66999','0');">IA100831-001</a>

<a href="javascript:view('67377','67377','0');">IA100904-004</a>

<a href="javascript:view('67378','67378','0');">IA100904-005</a>

<a href="javascript:view('3271','3271','0');">IA080115-031</a>
------------------
IA100908-002
IA100908-001
IA100908-016
IA100903-008
IA100903-007
IA100710-013
IA100515-013
IA100430-022
IA100830-001
IA100713-003
IA100713-001
IA100713-005
IA100830-010
IA100831-001
IA100904-004
IA100904-005
IA080115-031


补充下,input.html的基本结果如图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值