【使用JSOUP实现网络爬虫】获取所有链接

这个示例程序将展示如何从一个URL获得一个页面。然后提取页面中的所有链接、图片和其它辅助内容。并检查URLs和文本信息。

运行下面程序需要指定一个URLs作为参数


 
 
  1. import org.jsoup.Jsoup;  
  2. import org.jsoup.helper.Validate;  
  3. import org.jsoup.nodes.Document;  
  4. import org.jsoup.nodes.Element;  
  5. import org.jsoup.select.Elements;  
  6.   
  7. import java.io.IOException;  
  8.   
  9. /** 
  10.  * Example program to list links from a URL. 
  11.  */  
  12. public class ListLinks {  
  13.     public static void main(String[] args) throws IOException {  
  14.         Validate.isTrue(args.length == 1"usage: supply url to fetch");  
  15.         String url = args[0];  
  16.         print("Fetching %s...", url);  
  17.   
  18.         Document doc = Jsoup.connect(url).get();  
  19.         Elements links = doc.select("a[href]");  
  20.         Elements media = doc.select("[src]");  
  21.         Elements imports = doc.select("link[href]");  
  22.   
  23.         print("\nMedia: (%d)", media.size());  
  24.         for (Element src : media) {  
  25.             if (src.tagName().equals("img"))  
  26.                 print(" * %s: <%s> %sx%s (%s)",  
  27.                         src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),  
  28.                         trim(src.attr("alt"), 20));  
  29.             else  
  30.                 print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));  
  31.         }  
  32.   
  33.         print("\nImports: (%d)", imports.size());  
  34.         for (Element link : imports) {  
  35.             print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel"));  
  36.         }  
  37.   
  38.         print("\nLinks: (%d)", links.size());  
  39.         for (Element link : links) {  
  40.             print(" * a: <%s>  (%s)", link.attr("abs:href"), trim(link.text(), 35));  
  41.         }  
  42.     }  
  43.   
  44.     private static void print(String msg, Object... args) {  
  45.         System.out.println(String.format(msg, args));  
  46.     }  
  47.   
  48.     private static String trim(String s, int width) {  
  49.         if (s.length() > width)  
  50.             return s.substring(0, width-1) + ".";  
  51.         else  
  52.             return s;  
  53.     }  
  54. }  

示例输入结果


 
 
  1. Fetching http://news.ycombinator.com/...  
  2.   
  3. Media: (38)  
  4.  * img: <http://ycombinator.com/images/y18.gif> 18x18 ()  
  5.  * img: <http://ycombinator.com/images/s.gif> 10x1 ()  
  6.  * img: <http://ycombinator.com/images/grayarrow.gif> x ()  
  7.  * img: <http://ycombinator.com/images/s.gif> 0x10 ()  
  8.  * script: <http://www.co2stats.com/propres.php?s=1138>  
  9.  * img: <http://ycombinator.com/images/s.gif> 15x1 ()  
  10.  * img: <http://ycombinator.com/images/hnsearch.png> x ()  
  11.  * img: <http://ycombinator.com/images/s.gif> 25x1 ()  
  12.  * img: <http://mixpanel.com/site_media/images/mixpanel_partner_logo_borderless.gif> x (Analytics by Mixpan.)  
  13.    
  14. Imports: (2)  
  15.  * link <http://ycombinator.com/news.css> (stylesheet)  
  16.  * link <http://ycombinator.com/favicon.ico> (shortcut icon)  
  17.    
  18. Links: (141)  
  19.  * a: <http://ycombinator.com>  ()  
  20.  * a: <http://news.ycombinator.com/news>  (Hacker News)  
  21.  * a: <http://news.ycombinator.com/newest>  (new)  
  22.  * a: <http://news.ycombinator.com/newcomments>  (comments)  
  23.  * a: <http://news.ycombinator.com/leaders>  (leaders)  
  24.  * a: <http://news.ycombinator.com/jobs>  (jobs)  
  25.  * a: <http://news.ycombinator.com/submit>  (submit)  
  26.  * a: <http://news.ycombinator.com/x?fnid=JKhQjfU7gW>  (login)  
  27.  * a: <http://news.ycombinator.com/vote?for=1094578&dir=up&whence=%6e%65%77%73>  ()  
  28.  * a: <http://www.readwriteweb.com/archives/facebook_gets_faster_debuts_homegrown_php_compiler.php?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+readwriteweb+%28ReadWriteWeb%29&utm_content=Twitter>  (Facebook speeds up PHP)  
  29.  * a: <http://news.ycombinator.com/user?id=mcxx>  (mcxx)  
  30.  * a: <http://news.ycombinator.com/item?id=1094578>  (9 comments)  
  31.  * a: <http://news.ycombinator.com/vote?for=1094649&dir=up&whence=%6e%65%77%73>  ()  
  32.  * a: <http://groups.google.com/group/django-developers/msg/a65fbbc8effcd914>  ("Tough. Django produces XHTML.")  
  33.  * a: <http://news.ycombinator.com/user?id=andybak>  (andybak)  
  34.  * a: <http://news.ycombinator.com/item?id=1094649>  (3 comments)  
  35.  * a: <http://news.ycombinator.com/vote?for=1093927&dir=up&whence=%6e%65%77%73>  ()  
  36.  * a: <http://news.ycombinator.com/x?fnid=p2sdPLE7Ce>  (More)  
  37.  * a: <http://news.ycombinator.com/lists>  (Lists)  
  38.  * a: <http://news.ycombinator.com/rss>  (RSS)  
  39.  * a: <http://ycombinator.com/bookmarklet.html>  (Bookmarklet)  
  40.  * a: <http://ycombinator.com/newsguidelines.html>  (Guidelines)  
  41.  * a: <http://ycombinator.com/newsfaq.html>  (FAQ)  
  42.  * a: <http://ycombinator.com/newsnews.html>  (News News)  
  43.  * a: <http://news.ycombinator.com/item?id=363>  (Feature Requests)  
  44.  * a: <http://ycombinator.com>  (Y Combinator)  
  45.  * a: <http://ycombinator.com/w2010.html>  (Apply)  
  46.  * a: <http://ycombinator.com/lib.html>  (Library)  
  47.  * a: <http://www.webmynd.com/html/hackernews.html>  ()  
  48.  * a: <http://mixpanel.com/?from=yc>  ()  
阅读更多JSOUP相关文章,请看专栏:使用JSOUP实现网络爬虫
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值