zoukankan      html  css  js  c++  java
  • 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 + "---------个页面完成!
    ");
     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,还是有很多东西等待自己慢慢发现......

  • 相关阅读:
    Ts基础
    SWF 文件不能本地访问 只有仅限于文件系统的 SWF 文件
    laya的UI编辑器
    目前支持WebGL的浏览器有哪些?
    Laya学习
    egret学习
    PHP 获取上月,本月,近15天,近30天日期
    深入浅出:MySQL的左连接、右连接、内连接
    div中 li宽度不固定 ie6和ie7不兼容不自动换行
    8款给力HTML5/CSS3应用插件 可爱的HTML5笑脸
  • 原文地址:https://www.cnblogs.com/rememberme/p/Jsoup.html
Copyright © 2011-2022 走看看