jsoup测试例子

1、测试代码

 

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.parseUrl(); 
    } 
  
    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(); 
        } 
    } 
}

 

2、编译及执行

  1. javac -cp jsoup-1.7.1.jar:. Test.java
  2. java -cp jsoup-1.7.1.jar:. Test



3、测试结果

    1. <a href="http://www.baidu.com/gaoji/preferences.html" name="tj_setting">搜索设置</a>
    2. <a href="https://passport.baidu.com/v2/?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" id="lb" οnclick="return false;">登录</a>
    3. <a href="https://passport.baidu.com/v2/?reg&amp;regType=1&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2F" target="_blank" name="tj_reg" class="reg">注册</a>
    4. <a href="http://news.baidu.com">新&nbsp;闻</a>
    5. <a href="http://tieba.baidu.com">贴&nbsp;吧</a>
    6. <a href="http://zhidao.baidu.com">知&nbsp;道</a>
    7. <a href="http://music.baidu.com">音&nbsp;乐</a>
    8. <a href="http://image.baidu.com">图&nbsp;片</a>
    9. <a href="http://video.baidu.com">视&nbsp;频</a>
    10. <a href="http://map.baidu.com">地&nbsp;图</a>
    11. <a href="#" name="ime_hw">手写</a>
    12. <a href="#" name="ime_py">拼音</a>
    13. <a href="#" name="ime_cl">关闭</a>
    14. <a href="http://baike.baidu.com">百科</a>
    15. <a href="http://wenku.baidu.com">文库</a>
    16. <a href="http://www.hao123.com">hao123</a>
    17. <a href="http://www.baidu.com/more/">更多&gt;&gt;</a>
    18. <a id="seth" οnclick="h(this)" href="/" οnmοusedοwn="return ns_c({'fm':'behs','tab':'homepage','pos':0})">把百度设为主页</a>
    19. <a id="setf" href="http://www.baidu.com/cache/sethelp/index.html" οnmοusedοwn="return ns_c({'fm':'behs','tab':'favorites','pos':0})" target="_blank">把百度设为主页</a>
    20. <a href="http://www.baidu.com/search/baidukuaijie_mp.html" target="_blank" οnmοusedοwn="return ns_c({'fm':'behs','tab':'kuaijie','pos':1})">把百度添加到桌面</a>
    21. <a href="http://e.baidu.com/?refer=888" οnmοusedοwn="return ns_c({'fm':'behs','tab':'btlink','pos':2})">加入百度推广</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://www.baidu.com/gaoji/preferences.html" name="tj_setting">搜索设置</a>
    29. <a href="https://passport.baidu.com/v2/?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" id="lb" οnclick="return false;">登录</a>
    30. <a href="https://passport.baidu.com/v2/?reg&amp;regType=1&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2F" target="_blank" name="tj_reg" class="reg">注册</a>
    31. <a href="http://news.baidu.com">新&nbsp;闻</a>
    32. <a href="http://tieba.baidu.com">贴&nbsp;吧</a>
    33. <a href="http://zhidao.baidu.com">知&nbsp;道</a>
    34. <a href="http://music.baidu.com">音&nbsp;乐</a>
    35. <a href="http://image.baidu.com">图&nbsp;片</a>
    36. <a href="http://video.baidu.com">视&nbsp;频</a>
    37. <a href="http://map.baidu.com">地&nbsp;图</a>
    38. <a href="http://baike.baidu.com">百科</a>
    39. <a href="http://wenku.baidu.com">文库</a>
    40. <a href="http://www.hao123.com">hao123</a>
    41. <a href="http://www.baidu.com/more/">更多&gt;&gt;</a>
    42. <a id="setf" href="http://www.baidu.com/cache/sethelp/index.html" οnmοusedοwn="return ns_c({'fm':'behs','tab':'favorites','pos':0})" target="_blank">把百度设为主页</a>
    43. <a href="http://www.baidu.com/search/baidukuaijie_mp.html" target="_blank" οnmοusedοwn="return ns_c({'fm':'behs','tab':'kuaijie','pos':1})">把百度添加到桌面</a>
    44. <a href="http://e.baidu.com/?refer=888" οnmοusedοwn="return ns_c({'fm':'behs','tab':'btlink','pos':2})">加入百度推广</a>
    45. <a href="http://top.baidu.com">搜索风云榜</a>
    46. <a href="http://home.baidu.com">关于百度</a>
    47. <a href="http://ir.baidu.com">About Baidu</a>
    48. <a href="http://www.miibeian.gov.cn" target="_blank">京ICP证030173号</a>

 

转载于:https://www.cnblogs.com/licomeback/articles/3091167.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值