Jsoup的简单的使用示例

利用Jsoup中的相关方法实现网页中的数据爬去,本例子爬去的网页为比较流行的programmableweb中的mashup描述内容,然后为数据库中存在的mashup添加相应的描述。

  1 package com.test;
  2 
  3 import java.io.IOException;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 import org.jsoup.Jsoup;
  7 import org.jsoup.nodes.Element;
  8 import org.jsoup.select.Elements;
  9 import com.bean.mashup_tags_apis;
 10 import com.daoImpl.MashupDaoImpl;
 11 
 12 public class JsoupTest {
 13 
 14     /**
 15      * @param args
 16      */
 17     public static void main(String[] args) {
 18 
 19         List<String> mashupName = new ArrayList<String>();
 20         List<String> mashupDescription = new ArrayList<String>();
 21         MashupDaoImpl mashupDaoImpl = new MashupDaoImpl();
 22         List<mashup_tags_apis> mashup_tags_apis = mashupDaoImpl
 23                 .findAllmashup_tags_apis();
 24 
 25         try {
 26 
 27             // 获取网页内容,从第二页开始,第1页特殊处理
 28             for (int p = 220; p < 365; p++) {
 29                 System.out.println("正在爬取第" + p + "个页面........");
 30                 org.jsoup.nodes.Document doc = Jsoup.connect("http://www.programmableweb.com/mashups/directory/"
 31                                         + p).get();
 32 
 33                 // 通过ID获得需要的表格
 34                 Element content = doc.getElementById("mashups");
 35 
 36                 // 按照[href*=/mashup/]取得数据
 37                 Elements name = content.select("[href*=/mashup/]");
 38 
 39                 // 踢出版本信息
 40                 String RegexMatcher = "[\\d.]+";
 41 
 42                 // 向mashupName集合中添加名字
 43                 for (int i = 0; i < name.size(); i++) {
 44                     String Name = name.get(i).text();
 45                     if (name.get(i).hasText() && !Name.matches(RegexMatcher)) {
 46 
 47                         mashupName.add(Name);
 48                     }
 49                 }
 50 
 51                 // 取得描述信息
 52                 Elements description = content.getElementsByTag("p");
 53                 // 向mashupDescription集合中添加描述信息
 54                 for (Element descri : description) {
 55                     String Comment = descri.text();
 56                     if (p == 1) {
 57                         // 第一页处理方式(名字和描述都为空)
 58                         if (Comment != null && Comment.length() > 2) {
 59                             if (Comment != null) {
 60                                 mashupDescription.add(Comment);
 61                             }
 62                         }
 63                     } else {
 64                         // 从第二页开始处理方式,描述为空用NoDescriptions占位
 65                         if (Comment == null) {
 66                             Comment = "NoDescriptions";
 67                         }
 68                         mashupDescription.add(Comment);
 69                     }
 70 
 71                 }
 72 
 73                 // 更新数据库
 74                 for (int i = 0; i < mashupName.size(); i++) {
 75                     String Name = mashupName.get(i);
 76                     for (int j = 0; j < mashup_tags_apis.size(); j++) {
 77                         if (Name.equals(mashup_tags_apis.get(j).getName())) {
 78                             String destrcipString = mashupDescription.get(i);
 79                             if (Name != null && destrcipString != null) {
 80                                 if (!mashupDaoImpl.updateMashup_tags_apis(
 81                                         destrcipString, Name)) {
 82                                     System.out.println("更新失败!");
 83                                 }
 84                             }
 85                         }
 86                     }
 87                 }
 88 
 89                 // 清空集合爬取下一个页面
 90                 mashupDescription.clear();
 91                 mashupName.clear();
 92                 System.out.println("第---------" + p + "---------个页面完成!\n");
 93             }
 94         } catch (IOException e) {
 95             // TODO Auto-generated catch block
 96             e.printStackTrace();
 97         }
 98 
 99         // 显示输出查看是否正确
100         // for (int i = 0; i < mashupName.size(); i++) {
101         // System.out.println((i + 1) + " " + mashupName.get(i));
102         // }
103         //
104         // for (int j = 0; j < mashupDescription.size(); j++) {
105         // System.out.println((j + 1) + " " + mashupDescription.get(j));
106         // }
107         System.out.println("恭喜您,描述添加成功!");
108     }
109 }

这也是我第一次是使用Jsoup,还是有很多东西等待自己慢慢发现......

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于使用Java编写网络爬虫,我们可以使用Jsoup库来进行HTML解析和数据抓取。 首先,您需要在项目中引入Jsoup库。您可以在Maven或Gradle构建工具中添加以下依赖项: Maven: ```xml <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.13.1</version> </dependency> ``` Gradle: ```groovy implementation 'org.jsoup:jsoup:1.13.1' ``` 接下来,您可以使用Jsoup来获取和解析网页内容。以下是一个简单示例代码,演示如何使用Jsoup来爬取网页并提取所需的数据: ```java import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; public class WebCrawler { public static void main(String[] args) { try { // 使用Jsoup连接到目标网页 Document document = Jsoup.connect("http://example.com").get(); // 使用CSS选择器提取所需的元素 Elements links = document.select("a[href]"); // 遍历并打印提取到的元素 for (Element link : links) { System.out.println("Link: " + link.attr("href")); System.out.println("Text: " + link.text()); } } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们使用`Jsoup.connect()`方法连接到目标网页,并使用`document.select()`方法使用CSS选择器提取所有带有`href`属性的链接元素。然后,我们遍历这些链接元素,并打印出链接的URL和文本。 您可以根据自己的需求进一步扩展和修改代码来满足特定的爬虫需求。请注意,在编写爬虫时,请遵守相关网站的使用条款和政策,以避免违反规定。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值