基于Java的数据采集(二)

在上一篇文章《基于Java的数据采集(一)》http://www.cnblogs.com/lichenwei/p/3904715.html

提到了如何如何读取网页源代码,并通过group正则 动态抓取我们所需要的网页数据

现在来写下关于数据的存储,思路很简单,只需要在我们每次读取一个数据的时候,把数据存放在临时变量,然后插入数据库即可。

《基于Java数据采集入库(三)》:http://www.cnblogs.com/lichenwei/p/3907007.html

《基于Java数据采集入库(终结篇)》:http://www.cnblogs.com/lichenwei/p/3910492.html

先来建一个表:

DoMysql.java(数据库连接类,并提供插入数据的方法)

 1 package com.lcw.curl;
 2 
 3 
 4 import java.sql.Connection;
 5 import java.sql.DriverManager;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 
 9 
10 public class DoMySql {
11   
12     //定义MySql驱动,数据库地址,数据库用户名 密码, 执行语句和数据库连接  
13     public String driver = "com.mysql.jdbc.Driver";
14     public String url = "jdbc:mysql://127.0.0.1:3306/football";
15     public String user = "root";
16     public String password = "";
17     public Statement stmt = null;
18     public Connection conn = null;
19     
20     //创建一个插入数据的方法
21     public void datatoMySql(String insertSQl) {
22 
23         try {
24             try {
25                 Class.forName(driver).newInstance();
26             } catch (Exception e) {
27             28                 e.printStackTrace();
29             }
30             //创建连接
31             conn = DriverManager.getConnection(url, user, password);
32             //创建一个 Statement 对象来将 SQL 语句发送到数据库
33             stmt = conn.createStatement();
34         } catch (SQLException e) {
35             e.printStackTrace();
36         }
37         try {
38             //执行SQL 插入语句
39             stmt.executeUpdate(insertSQl);
40         } catch (SQLException e) {
41             e.printStackTrace();
42         }
43         try {
44             stmt.close();
45             conn.close();
46         } catch (SQLException e) {
47             e.printStackTrace();
48         }
49     }
50     
51 }

 

GetData.java(过滤数据类)

 1 package com.lcw.curl;
 2 
 3 import java.util.regex.Matcher;
 4 import java.util.regex.Pattern;
 5 
 6 public class GetData {
 7     
 8     /**
 9      * 
10      * @param regex 正则表达式
11      * @param content 所要匹配的内容
12      * @return
13      */
14     public String getData(String regex,String content){
15         Pattern pattern=Pattern.compile(regex, Pattern.CASE_INSENSITIVE);//设定正则表达式,不区分大小写
16         Matcher matcher=pattern.matcher(content);
17         if(matcher.find()){
18             return matcher.group();
19         }else{
20             return "";
21         }
22     }
23 
24 }

 

CurlMain.java主程序类:

 1 package com.lcw.curl;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.InputStreamReader;
 5 import java.net.URL;
 6 
 7 public class CurlMain {
 8 
 9     /**
10      * @param args
11      */
12     public static void main(String[] args) {
13 
14         try {
15             String address = "http://www.footballresults.org/league.php?league=EngDiv1";
16             URL url = new URL(address);
17             InputStreamReader inputStreamReader = new InputStreamReader(url
18                     .openStream(), "utf-8");// 打开地址,以UTF-8编码的形式返回字节并转为字符
19             BufferedReader bufferedReader = new BufferedReader(
20                     inputStreamReader);// 从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。
21 
22             GetData data = new GetData();
23             DoMySql mySql = new DoMySql();
24             String content = "";// 用来接受每次读取的行字符
25             int flag = 0;// 标志,队伍信息刚好在日期信息后面,则正则相同,用于分离数据
26             String dateRegex = "\\d{1,2}\\.\\d{1,2}\\.\\d{4}";// 日期匹配正则表达式
27             String teamRegex = ">[^<>]*</a>";// 队伍匹配正则表达式
28             String scoreRegex = ">(\\d{1,2}-\\d{1,2})</TD>";// 比分正则表达式
29             String tempDate="";
30             String teama="";
31             String teamb="";
32             String score="";
33             int i = 0;// 记录信息条数
34             String sql = "";
35 
36             while ((content = bufferedReader.readLine()) != null) {// 每次读取一行数据
37                 // 获取比赛日期信息
38                 String dateInfo = data.getData(dateRegex, content);
39                 if (!dateInfo.equals("")) {
40                     System.out.println("日期:" + dateInfo);
41                     tempDate=dateInfo;
42                     flag++;
43                 }
44                 // 获取队伍信息,需先读到日期信息让标志符自增
45                 String teamInfo = data.getData(teamRegex, content);
46                 if (!teamInfo.equals("") && flag == 1) {
47                     teama = teamInfo.substring(1, teamInfo
48                             .indexOf("</a>"));
49                     System.out.println("主队:" + teama);
50                     flag++;
51                 } else if (!teamInfo.equals("") && flag == 2) {
52                     teamb = teamInfo.substring(1, teamInfo
53                             .indexOf("</a>"));
54                     System.out.println("客队:" + teamb);
55                     flag = 0;
56                 }
57                 // 获取比分信息
58                 String scoreInfo = data.getData(scoreRegex, content);
59                 if (!scoreInfo.equals("")) {
60                     score = scoreInfo.substring(1, scoreInfo
61                             .indexOf("</TD>"));
62                     System.out.println("比分:" + score);
63                     System.out.println();
64                     i++;
65                     sql = "insert into football(`date`,`teama`,`teamb`,`score`) values('"
66                             + tempDate
67                             + "','"
68                             + teama
69                             + "','"
70                             + teamb
71                             + "','"
72                             + score + "')";
73                     System.out.println(sql);
74                     mySql.datatoMySql(sql);
75                 }
76 
77             }
78             bufferedReader.close();
79             System.out.println("一共收集到了" + i + "条信息");
80         } catch (Exception e) {
81             e.printStackTrace();
82         }
83 
84     }
85 
86 }

 

看下运行效果图:

 

下一篇文章:《基于Java的数据采集(三)》:http://www.cnblogs.com/lichenwei/p/3905370.html

posted @ 2014-08-11 18:17  李晨玮  阅读(4468)  评论(0编辑  收藏  举报