国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

簡(jiǎn)單java采集程序一

2019-11-14 21:03:00
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
簡(jiǎn)單java采集程序一

【目標(biāo)任務(wù)】通過(guò)該網(wǎng)站采集全國(guó)的手機(jī)號(hào)碼段至數(shù)據(jù)庫(kù)表中

【完成過(guò)程】

1、初涉正則表達(dá)式,學(xué)會(huì)寫簡(jiǎn)單的正則表達(dá)式

2、獲取單個(gè)網(wǎng)頁(yè)內(nèi)容,學(xué)會(huì)java中基本的IO流

3、將獲取數(shù)據(jù)插入MySQL數(shù)據(jù)庫(kù)表中,掌握基本的JDBC編程。

5、通過(guò)url拼接獲取每一個(gè)城市的完整url

6、采集整個(gè)網(wǎng)站的號(hào)碼段,并使用批處理+預(yù)編譯批量插入數(shù)據(jù)庫(kù)表中

7、使用StringBuilder進(jìn)行優(yōu)化提速

【數(shù)據(jù)庫(kù)表】注意如果是在cmd命令下建表的話,字段名稱不需要加引號(hào)

create table number_segment (`id` bigint not null auto_increment unique,`segment` char(7) not null PRimary key,`province` varchar(255) not null,`city` varchar(255) not null) default charset=utf8;                                  

CE@PZ(2L[]0RZGNR6XS(4UH

【初涉正則表達(dá)式】

1、學(xué)習(xí)簡(jiǎn)單的表達(dá)式:正則表達(dá)式30分入門

2、在線測(cè)試自己寫的表達(dá)式:在線測(cè)試正則表達(dá)式1

3、使用java的Pattern類 和 Matcher類

import java.util.regex.Matcher;import java.util.regex.Pattern;public class test_ZhengZe{public static void main(String[] args){Pattern p = Pattern.compile("(13//d{5}[^<])");String s ="/mobile/guangzhou_1300040.>1300040</a></li><li><a href=/"../../mobile/guangzhou_1300041.html/">1300041</a></li><li><a ";Matcher m = p.matcher(s);while(m.find()){System.out.println("打印出的號(hào)碼段落:"+m.group(0));}System.out.print("捕獲的數(shù)據(jù)有:"+m.groupCount());}}

clipboard

 

【獲取網(wǎng)頁(yè)內(nèi)容】

這里主要用到  InputStream  BufferReader兩個(gè)IO流處理類。更多的方法講解見(jiàn)【java獲得采集網(wǎng)頁(yè)內(nèi)容的方法小結(jié)

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.URL;import java.util.regex.Matcher;import java.util.regex.Pattern;public class getHtml {    public static void main(String[] args) throws Exception    {    long start= System.currentTimeMillis();        String str_url="http://www.hiphop8.com/city/guangdong/guangzhou.php";        //匹配號(hào)碼段        Pattern p = Pattern.compile(">(13//d{5}|15//d{5}|18//d{5}|147//d{4})<");        String html = get_Html(str_url);        Matcher m = p.matcher(html);             int num = 0;       while(m.find()){System.out.println("打印出的號(hào)碼段落:"+m.group(1)+"  編號(hào)"+(++num));}       System.out.println(num);              long end = System.currentTimeMillis();System.out.println("花費(fèi)的時(shí)間"+(end-start)+"毫秒");    }     public static String get_Html(String str_url) throws IOException{    URL url = new URL(str_url);    String content="";StringBuffer page = new StringBuffer();try {BufferedReader in = new BufferedReader(new InputStreamReader(url                    .openStream()));while((content = in.readLine()) != null){page.append(content);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}        return page.toString();    }}
【將采集內(nèi)容插入數(shù)據(jù)庫(kù)】

java連接mysql數(shù)據(jù)庫(kù)的大概操作是:

加載mysql驅(qū)動(dòng)---》創(chuàng)意一個(gè)數(shù)據(jù)庫(kù)連接---》創(chuàng)建一個(gè)sql語(yǔ)句執(zhí)行對(duì)象statement---》定義String類型的SQL語(yǔ)句,statment調(diào)用SQL語(yǔ)句的執(zhí)行方法---》關(guān)閉statment對(duì)象和數(shù)據(jù)庫(kù)。

import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;public class database {public static String driver ="com.mysql.jdbc.Driver";public static String url    ="jdbc:mysql://127.0.0.1:3306/tele_dat?autoReconnect=true&characterEncoding=UTF-8";public static String user   ="root";public static String passWord = "123456";public static Statement statement = null;public static java.sql.Connection conn = null;public static int i=0;//創(chuàng)建一個(gè)插入數(shù)據(jù)的方法public static void datatoMySql(String sql) throws SQLException {try {Class.forName(driver);} catch (ClassNotFoundException e) {System.out.println("加載驅(qū)動(dòng)失敗");e.printStackTrace();}conn = DriverManager.getConnection(url, user, password);//創(chuàng)建一個(gè)連接statement = conn.createStatement();   //創(chuàng)建一個(gè)Statemnet對(duì)象來(lái)傳送SQL語(yǔ)句statement.executeUpdate(sql);}public  static void close() throws SQLException{statement.close();   //關(guān)閉數(shù)據(jù)庫(kù)操作對(duì)象conn.close();       //關(guān)閉數(shù)據(jù)庫(kù)連接}//測(cè)試連接數(shù)據(jù)庫(kù)例子public static void main(String args[]){String sql = "insert  into   number_segment(segment,province,city) " +"values (123458,'廣東1','廣州') ";try {datatoMySql(sql);System.out.println("插入成功");} catch (SQLException e) {System.out.println("插入失敗");e.printStackTrace();}try {close();System.out.print("關(guān)閉數(shù)據(jù)庫(kù)");} catch (SQLException e) {e.printStackTrace();}}}

我用的是wampsever中集成的mysql數(shù)據(jù)庫(kù),并在cmd下進(jìn)行操作,常用命令見(jiàn):mysql常用的命令見(jiàn),如果對(duì)jdbc編程不熟,可以參考這篇博文

 

【獲取整個(gè)網(wǎng)站中所以城市的URL】

通過(guò)查看網(wǎng)站首頁(yè)的源代碼,發(fā)現(xiàn)可以從這里獲取每一個(gè)省份的URL,然后觀察一個(gè)省份的頁(yè)面,可以獲取該省城市url的部分后綴,由此可以拼接就得到一個(gè)完整城市的url.

mport java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.URL;import java.util.ArrayList;import java.util.regex.Matcher;import java.util.regex.Pattern;public class get_all_city_url {public static void  main(String[] args) throws Exception {String home_url = "http://www.hiphop8.com";String pattern_pro ="//w{3}//.//w{7}//.//w{3}/////w{4}/////w+";  //匹配省份的URLString pattern_city_hz="<LI><A href=/"(.*?)/" target=_blank>";   //城市后綴Matcher mat_home = get(home_url,pattern_pro);int i = 0;//可以用ArrayList保存所有url,另外可以用StringBuilder對(duì)字符串進(jìn)行相加,不過(guò)測(cè)試耗時(shí)差不多l(xiāng)ong  start = System.currentTimeMillis();while(mat_home.find()){String city_url_qz = "http://"+mat_home.group()+"/";Matcher mat_city_hz = get(city_url_qz,pattern_city_hz);while(mat_city_hz.find()){i++;String city_url = city_url_qz + mat_city_hz.group(1);System.out.println(i+"  "+city_url);}}long  end = System.currentTimeMillis();long time =end - start;System.out.println("總共用時(shí)"+time);}public static  Matcher get(String str, String pa) throws Exception  {String urlsource =get_Html(str);Pattern p = Pattern.compile(pa);Matcher m = p.matcher(urlsource);return m;}public static String get_Html(String str_url) throws IOException{    URL url = new URL(str_url);    String content="";StringBuffer page = new StringBuffer();try {BufferedReader in = new BufferedReader(new InputStreamReader(url                    .openStream()));while((content = in.readLine()) != null){page.append(content);}} catch (IOException e) {e.printStackTrace();}        return page.toString();    }} 

 

3DB6A7ACC5B647B49F182322CEF7B13C

 

       有了上面的基礎(chǔ),就可以做整個(gè)網(wǎng)站的號(hào)碼段采集了,不過(guò)后面因?yàn)椴迦霐?shù)據(jù)表中的數(shù)據(jù)是20多萬(wàn),所以更多的是要考慮效率問(wèn)題了,后面的部分見(jiàn)簡(jiǎn)單的java采集程序二

      另外網(wǎng)上有不少的java采集教程,有些寫的很棒,自己深知java打的太少了,寫這篇博文一是為了總結(jié),留個(gè)紀(jì)念,二是希望能給和我一個(gè)的初學(xué)者一些幫助,和大家一起交流探討,當(dāng)然如果文章有寫的不對(duì)的地方,還希望大神指出。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 休宁县| 石阡县| 武功县| 长寿区| 新巴尔虎右旗| 开鲁县| 炎陵县| 闻喜县| 九江市| 新闻| 凤凰县| 二连浩特市| 溧阳市| 交口县| 荥经县| 吉隆县| 东平县| 永定县| 罗定市| 伊宁市| 冀州市| 江源县| 密山市| 东明县| 余干县| 宁陵县| 湖州市| 丽水市| 南江县| 沙河市| 长顺县| 卢龙县| 尼木县| 商水县| 宣恩县| 米林县| 大邑县| 偏关县| 建水县| 平陆县| 雷山县|