在上一篇文章《基於Java的數據採集(一)》:http://www.cnblogs.com/lichenwei/p/3904715.htmlphp
提到了如何如何讀取網頁源代碼,並經過group正則 動態抓取咱們所須要的網頁數據html
如今來寫下關於數據的存儲,思路很簡單,只須要在咱們每次讀取一個數據的時候,把數據存放在臨時變量,而後插入數據庫便可。java
《基於Java數據採集入庫(三)》:http://www.cnblogs.com/lichenwei/p/3907007.htmlmysql
《基於Java數據採集入庫(終結篇)》:http://www.cnblogs.com/lichenwei/p/3910492.html正則表達式
先來建一個表:sql
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主程序類:curl
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