本文没有采取通用的爬虫算法,而是通过对论坛URL结构和网页内容显示框架进行分析后编写的对大多数论坛都通用的爬虫算法,这也为使得论坛数据的获取变得相对容易一些。
本文通过对用户所发帖子、精华帖子、加分帖子、别人回复的帖子的数量进行分析,对帖子数量利用极差分析法规范化后加权求用户得分,从而确定得分最高的用户就是言论领袖;通过对用户总在线时间和发帖数、回帖数利用极差分析法进行规范并加权求和后得到活跃度较高的用户。
对于话题用户的确定和关系圈的挖掘,本文利用向量空间模型,把用户所发帖子内容表示成文档向量形式,通过相似性计算对文本(帖子)聚类,并最终确定人际关系圈和话题用户。
******************************************************************************
数据获取方法(爬虫算法介绍)(图片我都没贴上,因为我做题是在我另一台电脑上,图片在那台电脑上,word直接复制不来图片)
论坛数据的获取在本文要解决的问题中是一个很重要的问题。由于数据挖掘要求数据必须是结构化的,所以本文先把论坛上的数据通过一定的策略获取并转移到关系型数据库中。+ b1 Q# ]# M( `! f: ]; r/ S! y6 [
本文采用java语言从论坛目标网页上实现数据获取,关系型数据库选用SqlServer数据库。
为便于描述,将数据库表结构罗列如下:Topic表: id1(主键) 、tid(帖子在论坛上的ID) 、url(帖子的入口URL)、 uid(发帖人在论坛上的ID) 、jh(帖子是否精华)、jf(帖子是否被加分)Commentb表:id2(主键) 、tid(帖子在论坛上的ID) 、uid(发帖人在论坛上的ID)、suid(回帖人在论坛上的ID) 、title(帖子题目) 、ttext(帖子内容) 、stext(回帖内容)BbsUser表:id(主键) 、uid(发帖人在论坛上的ID) 、name(发帖人的昵称) 、ontm(用户在线时间) 、ft(发帖数) 、ht(回帖数) 、zhf(别人总回复数) 、zjh(总精华数) 、 zjf(总加分数)从以上数据库表结构中可以看出,发帖与跟帖是一对多的关系。现如今,多数论坛都采用开源框架,比如国内比较著名的discuz和phpwind,而discuz最受欢迎,但是由于论坛的原理都是一样的,所不同的只是编码的方式、外在的表现以及局部的细节,因此针对一个论坛所编写的爬虫程序经过些许改动就能在另外一个相似的论坛上面运行,考虑到discuz用户数众多,因此程序只针对discuz7.2版本开发,而题目中所提到的四个论坛有三个是用discuz,本文选择http://diybbs.it168.com开发爬虫程序。如果让爬虫程序把从网页上面读取到的字符流经过处理直接存进数据库,由于页面众多,这样的处理过程实际上严重增加了网络开销,程序要运行很长时间。比较好的做法是,把在网页上读到的字符流经过初步简单处理,也就是用正则表达式匹配包含要收集的数据源信息,然后将数据写入多个文本文件(可以采取一个论坛的版块一个文本文件的方式)(参见TextCr.java),这个时候不把数据存入数据库而是存入文本文件的原因是此时只是初步处理了网络上的字符流而还没有获取最终需要的数据,因此先写进文本文件,然后再写个程序(参见TopicDb.java)从这些文本文件读取数据存入数据库中,因为这时候程序没有网络开销,所以这时候进行复杂的处理也不会对程序运行时间产生很大的影响。
以上的过程也是分布进行的。具体的说,是先从论坛上所有版块往板块内部抓取,可以设置一个抓取深度,因为毕竟一个论坛上的数据量是很惊人的,我们可以选取最近一段时间的帖子来抓取。为方便,本文选取深度为5(也就是从论坛每个版块首页网版块内部抓取5个网页为止)。
对于http://diybbs.it168.com来说,就是http://diybbs.it168.com/forum/-i-j.html其中的i代表第i个版块,j代表页面深度,http://diybbs.it168.com一共有将近200个版块(i最大是212,但是零到212中间有的版块不存在),j在本文中取1,2,3,4,5从上图可以看到从版块页面(也就是还没进到帖子内部去看),可以看到帖子的标题、回复次数、是否精华、是否被加分、发帖人信息。这些信息恰好组成了Topic数据库表的字段名,因此可以先把论坛上的这些信息抓取下来,经过初步处理(正则表达式匹配),写进文本文件。文本文件截图为:其中文件名为版块在网页上的ID,一共到212,但是中间并不是全部都有,比如从上图可以看出6.txt不存在。文件内部结构如图:
从图中可以看出,文本文件内部还是包含了太多无用的信息,而要插进数据库只是很少一部分有用的数据。用java语言读取文本信息并提取出有用的信息存进数据库,具体过程可参见附录程序代码。现在还只是把帖子的标题、ID、URL、回复数、作者等信息存进了数据库,而帖子内容还没有获得。应该再从数据库中一次性读出所有帖子(取样也可)的Url,放进计算机内存,然后再次编写爬虫程序(参见CrawlComment.java)去读取帖子内容以及回复内容、回复人的信息等。这次爬虫的处理也还是先把网络上的字符流初步处理下存进文本文件,最终通过对文本文件的处理获取需要的数据存进数据库(参见OtherDb.java)。最终数据库Topic表信息如下图所示:& W+ T$ S* k6 h
模型里面公式不好弄,我也不贴了。
给出代码吧,如果想学习,可以拿去学习下。(只是java代码OtherDb.java程序部分功能没实现,当时要急着写论文,就没去深究。这两天忙着别的事情,也没完成。如果你有兴趣,可以完成下。具体是写进表comment没实现)
附录1TextCr.java 用于抓取Topic数据库表代码package huazhong; import java.io.File;import java.io.FileWriter;import java.io.FileNotFoundException;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.Date;import java.util.regex.Matcher;import java.util.regex.Pattern; public class TextCr {% K& N5 z4 t7 |/ U1 ~$ ]/ m
publicstatic void main(String[] args) {4 p: M% x; V0 `% r
File
dirFile;
dirFile= new File("E:\\HTopic");' {; l6 }0 T* c
if(!dirFile.exists()){
dirFile.mkdir();
}
Datestarttime=new Date();; [4 _# }& c( L; t8 D
inti,j;; `3 q. I: x: U9 ^5 i
for(i=1;i<213;i++){2 P6 i' r* L# L; x+ Y/ H
FileWriterfw=null;
Stringtext="";
String% }4 a) R: u& g7 y2 J6 g Z
path=null;
for(j=1;j<11;j++){//每个版块挖掘深度为10% k1 Z) Y5 r: [
StringurlString="http://diybbs.it168.com/forum-"+i+"-"+j+".html";
StringBufferhtml_text = new StringBuffer();
7 a) k" g2 o1 W) u
try {# b0 g$ R4 O7 m
8 L; X) o: z6 r2 N
6 I5 T% `4 C% @- j4 S" i
URLurl = new URL(urlString);& S2 x+ J, D5 S# r0 e ^
URLConnection conn =url.openConnection();
BufferedReader reader = newBufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
9 |) o/ }8 j' ~' s
while ((line = reader.readLine()) !=null){
html_text.append(line);//不分行,这样更加容易处理9 f6 [; Y/ l1 z
# r2 Y( \5 _ P, q- W) H
}0 A. f$ P4 \& [6 C3 X
reader.close();
}catch (FileNotFoundException e){) y* ^" ~6 N# P7 j$ J% m+ ?) P
continue;1 d! Z2 [3 N/ l( H
% o! u, h! S0 [2 L4 H
}
catch(MalformedURLException e) {! u5 d; {, e7 p3 k
System.out.println("无效的URL: " + urlString);2 d+ n7 r6 _2 k0 {8 Y
}catch (IOException e) {
e.printStackTrace();# S |0 b; L- u9 ?
}
& \" F" A9 l. j+ Y
Patternpat = Pattern.compile("<th class=\"subject (.+?)<tdclass=\"lastpost\">");
Matchermat=pat.matcher(html_text);
if(!mat.find()){& R' m7 P, D0 f* r7 [* u, O
continue;
}
while(mat.find()){$ g; D' D% H% g( ^1 s* L# x
Stringstrr=mat.group();
System.out.println(strr);2 R7 _! W. F7 k+ N- Q" Q+ U
9 X9 @% T3 ?; `3 I
text+=strr+"\n";
}
}
if(text.equals("")){
continue;
}1 `" G( C% D' q
try{+ |$ e8 M8 Z! I* {
path=dirFile+"\\"+i+".txt";
Filefile = new File(path);# y- ^8 Z! C9 i7 H* p& l! ]6 D% f
if(!file.exists())( ^/ q. A. P H3 \+ M. S+ a) S
file.createNewFile();
fw=newFileWriter(file);5 b: F+ f+ K3 z& \. F( h
fw.write(text);( L7 @( J1 \( X1 Q: i r
}catch(IOException e){/ g9 I7 }- l( m! Q9 N. Q- e
System.out.println("输入输出异常");
5 t$ |, w! j1 z; G
}finally{' @, D; W+ s; c
if(fw!=null)( |1 S" \: |: B" \% m
try{fw.close();}catch(IOException e){}}
}% s' r* x: n, F& E9 `
Dateendtime=new Date();
longtime=endtime.getTime()-starttime.getTime();
System.out.println("用时:"+time+"ms");
}} 附录2 TopicDb.Java将上面程序得到的文本文件写进数据库package huazhong; import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;import java.util.Date;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException; public class TopicDb {4 ?/ ]) q* A/ Y7 D6 o
publicstatic void main(String[] args) {! Y- O% w) T1 | H
Datestarttime=new Date();
inttidMark=1;
inti,hf,uid;* x7 I* I6 W- P' f/ q' w
intpreuid,maxpage;& Q. @' R3 l0 q; C9 G5 _3 U }- ?/ U
Stringurl=null;9 ~- N p+ Y: ]' W2 ~
StringmainUrl="http://diybbs.it168.com/";
Connection connection=null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection= DriverManager.getConnection(% @+ K5 Y! p1 X) O
"jdbc:sqlserver://localhost:1433;DatabaseName=bbsdb",
"sa","123");% t$ n- a: M; x# Q& _
}catch(Exception e) {* R; n4 A7 T& f0 \
System.out.println(e.getMessage());
}
Stringpath="E:/HTopic/";
Filef = new File(path);# x$ J. Q7 X% b6 e9 h
File[]list = f.listFiles();- w% ]: k" |: K0 c1 s& F
for(i=0;i<list.length;i++){
try{
FileReaderreader = new FileReader(list);* J/ r' c. J4 y
BufferedReaderbr = new BufferedReader(reader); , c: d6 D( t6 Z: A/ L( o- N
Stringstring = null;* b$ \- ]) I# H% |
while((string= br.readLine()) != null) {
" I. T- G- ?, h m
intjh=0,jf=0;- J" N- x+ [" z2 w8 K* K
if(string.indexOf("精华")!=-1){! Y8 x# l* S6 ?( C
jh=1;
}3 C! T( S, b; f7 p7 @8 c( p
if(string.indexOf("加分")!=-1){
jf=1;* |# h1 ?2 t% V8 {4 ?7 s6 S( p
}/ t7 }/ x( k: a) k
if(string.indexOf("匿名")!=-1){
continue;3 v! t# q5 M" `4 `
}
Stringstring2 ="<span id=\"thread_";, x) T6 F8 k) M$ y, Y( v+ C
inta=string2.length();
intb=string.indexOf(string2);# [8 L8 @4 \2 C% [" k; T. h
intc=string.indexOf("\"",b+a);% v5 z0 N& J2 Z1 z: {9 q0 N
Stringstring3 = string.substring(a+b, c);2 G+ G) ]3 }& R/ p0 }
preuid=Integer.parseInt(string3);
url=mainUrl + "thread-"+string3+"-1-1.html";
intd=string.lastIndexOf("</a></span>");
inte=string.lastIndexOf(">",d);' ~0 P, u1 j3 y- Z9 ]. b. K6 M
Stringstring4 =string.substring(e+1,d);
try{
maxpage= Integer.parseInt(string4);7 o6 X: Z# a: a4 j, V8 J5 H
}catch(NumberFormatException e2) {/ I8 y1 \1 i" |9 q8 b7 L& K
maxpage=1;6 Q; h6 ]) y/ m
}
intm=string.indexOf("<strong>");" e3 w1 c6 H# }& {6 Z: g8 Q
intt="<strong>".length();
intn=string.lastIndexOf("</strong>");
Stringstring5 =string.substring(m+t,n);. ^" l: q* K6 d0 n- i1 h0 p
try{: V. e4 x4 ^" v3 Z& R
hf=Integer.parseInt(string5);
}catch(NumberFormatException e3) {
hf=0;
} [( T( T6 n( P7 b3 R7 |
String string6="<ahref=\"space-uid-";; O6 t$ {8 H+ Z" F0 M- d
intx=string.indexOf(string6);0 ?" C' _8 s. F' I" O
inty=string6.length();
intz=string.indexOf(".",x);
Stringstring7 =string.substring(x+y,z);: W! W& \3 e8 L9 `9 b
uid=Integer.parseInt(string7);
try{, k: r( ^8 U9 V5 O5 ~6 ~
Statementstmt=connection.createStatement();2 J+ ^3 E. B2 x" }4 N* p2 a
ResultSetres = stmt.executeQuery("select tid from Topic where tid="+preuid);7 M( m) d; J; I( {
if(res.next()){
continue;
}
PreparedStatementpstmt=null; C" v; I* W0 v4 k& i' U/ a
Stringexpr="insert into Topic (id1,tid,url,uid,jh,jf,hf,maxpage) values(?,?,?,?,?,?,?,?)";: k; w/ \1 }9 x9 | s2 Z
pstmt=connection.prepareStatement(expr);
pstmt.setInt(1,tidMark);
pstmt.setInt(2,preuid);) c; C( z& \) U* ^3 n3 h
pstmt.setString(3,url);+ N. a* m: f$ R( V. p1 E
pstmt.setInt(4,uid);
pstmt.setInt(5,jh);
pstmt.setInt(6,jf);
pstmt.setInt(7,hf);
pstmt.setInt(8,maxpage);+ U/ Q. u% W" g, `" o# I. M
pstmt.executeUpdate();
}catch(Exception e1) {
System.out.println(e1);2 c0 v7 E, f* l2 T" X
}/ m: y" A$ X0 `# }; ?; f
tidMark++;
}}catch(IOExceptione){* D' z0 R0 E0 x# g I) ` G4 o
System.out.println("读取文件异常");$ r4 P; s* n$ o+ |! j" E2 n
}
}
# ?. \" v0 g4 J
try{* w# M: S6 v: S
connection.close();& U5 B$ `9 S6 o
}catch(Exception e) {
System.out.println(e);+ x" N1 Q# i& v* z$ a6 e
}
Dateendtime=new Date();
longtime=endtime.getTime()-starttime.getTime();. m* k. N/ t" x6 ?5 y0 r. D/ P' j5 J6 R% k
System.out.println("用时:"+time+"ms");, _. X6 v* K3 F! p+ y2 t
}} 附录3CrawlComment.java 抓取回复的帖子到本地文件package huazhong; import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.util.Date;import java.util.regex.Matcher;import java.util.regex.Pattern; public class CrawlComment {" h$ M L x+ |# ?; ~( U5 u
publicstatic void main(String[] args) {
Datestarttime=new Date();
File
dirFile;
StringurlMain="http://diybbs.it168.com/";: M! V k% j, n1 D. k, q. x# J. k
inttid,maxpage; z+ w4 R: t* ?
int[]tidArray=new int[3000];
int[]maxArray=new int[3000];' i8 ]8 l. R" v" H0 A
dirFile= new File("E:\\HComment");
# Y8 K c: P {4 E' o) |* r, W
if(!dirFile.exists()){- l& [" Z! V! _, l; j, J, p
dirFile.mkdir();% H( e% T/ }; Q0 a( J% |7 Y e
}
Connectionconnection=null;; m$ [) h$ n, ~! p
try{" ^6 |7 R" ~' _0 P0 j3 O8 r4 v6 z
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");' ^ D* s$ u, h; s; M: ]* D% l; B
: Z& J. S1 u$ I6 T$ o7 C" m
connection= DriverManager.getConnection(% s; G M# v0 u
"jdbc:sqlserver://localhost:1433;DatabaseName=bbsdb", s7 Q% v! C& }' e
"sa","123");! G! t: M9 G9 W1 e& V
Statementstatement=connection.createStatement();
Stringexpr="select id1,tid,maxpage from Topic where id1>20";/9 |9 a+ M: @1 \7 K7 @ W- k
ResultSetresultSet =statement.executeQuery(expr);
while(resultSet.next()){6 P2 J! C3 J* w, ]* }) `, A+ W! W1 F
intid1=resultSet.getInt("id1");
tid=resultSet.getInt("tid");: r- |3 N5 c6 p' m8 \, c
maxpage=resultSet.getInt("maxpage");. a2 l8 f9 U2 M% D. Y- B$ x
tidArray[id1]=tid;
maxArray[id1]=maxpage;7 m* ]$ o: W. M# i5 {
}try{
connection.close();; }2 @6 i5 t: Z' r) p( B
}catch (Exception e) {- b: k w/ J: E0 P+ C( y
System.out.println(e);
}
}catch(Exception e) {. A9 F9 [' N+ @( _- O7 E5 r" t
System.out.println(e);
}) F5 J) K1 N' i0 O: L) B
inti,j;2 T* E! M: D8 F7 Z. R" k$ x! f
for(i=21;i<1001;i++){
Stringtitle="";& P% t5 y0 u% Q3 K
FileWriterfw=null;
Stringtext="";
String O7 Z$ \& C; o* L; |
path=null;
if(maxArray>2){
maxArray=2;) v" o: p) w+ v5 x& `( {# N
}
try{% \8 G( A+ ~% t0 r* p7 l
StringurlString=urlMain+"/thread-"+tidArray+"-"+1+"-1.html";
StringBufferhtml_text1 = new StringBuffer();
try{; H# Z) ]) U6 h; b7 ^: ?% V
URLurl = new URL(urlString);
URLConnectionconn1 = url.openConnection();+ e3 C6 g" E7 S% @
BufferedReaderreader = new BufferedReader(new InputStreamReader(conn1.getInputStream()));
Stringline = null;( _) O' G9 X/ [& j* J* j
while ((line = reader.readLine()) !=null){# `7 c1 T0 \2 C# H% U. O# e1 D
k) e( k6 ?- P! q
if(line.contains("h1")){
5 V( N4 W/ f2 g& k4 x
inta=line.lastIndexOf("<");9 [1 B& G. W; q1 L8 C/ u
4 M4 p0 D: d& o- s5 b
, N, W. I1 K/ M, A$ ?
int b=line.lastIndexOf(">",a);
. F: r, |( K% }( J' ~/ v; L' k; L
title=line.substring(b+1,a);$ `. V8 D, j. T9 H% F' {
. S( _4 {( V0 E
. L& k) H9 C( l2 Z8 J, q
}$ V7 B6 I A- \( }0 w
% `9 a8 j1 m; A& K* O: z9 e: s
html_text1.append(line);7 `# a) {8 D1 E# } j( x: _" _
}
if(html_text1.indexOf("管理员")!=-1){//||html_text1.indexOf("版主")!=-1
6 w( Z5 e) H% n' E+ i. E
7 p% j* g3 S5 `: K% Y- o
continue;2 `4 x* @2 r8 U# e
3 U' [: Y9 \! q: I1 H6 U& U8 J9 h
}
reader.close();7 V" v ~; b( | Q
Pattern pat =Pattern.compile("<divclass=\"popuserinfo\">(.+?)</td></tr></table>");
Matcher mat=pat.matcher(html_text1);
9 g7 _3 K- T! y9 |
if(!mat.find()){/ p5 K) [% S0 ]; z' i" u
4 w* `) A' b) {2 R* q" Z0 X
" x5 |' I2 T( Y) @
continue;
} c& @" U8 _: X, {2 @) B0 w
. z2 \- u* X6 j3 E
while(mat.find()){
* I2 J6 z# ~/ z; W, f! [
Stringstrr=mat.group();
3 X6 {& V9 t- L3 P b
5 ?) P0 E! G6 d- ]
System.out.println(strr);& W$ `1 ]# @6 t# h4 o
" t. r; j! Y7 J) z, c
text+=strr+"\n";1 n: i p# \7 p4 X# i1 w1 a4 K- U p
6 E# I* X: u9 L [2 [9 e8 h+ {+ o
}8 c. W5 ~9 k, O6 K6 k: y& o/ G
2 X& Z* H: ?% ~% @
}catch (FileNotFoundException e){
0 p" B2 e$ ?; J7 M1 n- j5 l
continue;3 N3 e$ ^( f( \$ Y
! D$ n- v* Y4 U" N1 k+ \
4 E& K) z) c8 D2 K
}! Y! r) u. @% y9 Z
catch (MalformedURLException e) {" C: t8 T8 l7 O) H7 m
System.out.println("无效的URL: " + urlString);0 b9 Q: W. K1 f, s. y
+ m* } C1 y+ [3 C2 u* _+ v
}
}catch(Exception e) {" y3 V2 c/ X% u/ Q& I5 ~
e.printStackTrace();+ Q, _1 i3 v1 ~) @ [/ _1 m
}
for(j=2;j<maxArray+1;j++){
StringurlString=urlMain+"/thread-"+tidArray+"-"+j+"-1.html";
StringBufferhtml_text = new StringBuffer();- Y% ^" o, f+ D+ q/ r: J. O, U" Q0 r- Q: P
try{
URLurl = new URL(urlString);) B& I$ \- @6 T% D1 m7 P4 N1 U3 T D
URLConnection conn =url.openConnection();
3 }2 C% M" h5 y3 R
4 F0 n, K* {1 A! r
BufferedReader reader = newBufferedReader(new InputStreamReader(conn.getInputStream()));5 l+ o' }; ~0 |6 j( P
' ?" M& I7 p' ~7 V; n
String line = null;* ~; m3 |8 z* o: o
, i/ ` f( w6 a8 }+ z
while ((line = reader.readLine()) !=null){
9 r7 s2 Q5 ~- F) q8 v+ z
# {3 Y8 I8 H, T7 a" n7 q1 k
html_text.append(line);
$ b% t1 u" n! E* O k; O4 v' Q
}
6 ?% p n' Q, [# ]: p% ]
reader.close();
5 n6 z8 k* S4 ~4 k" e
/ J5 ?' S, u3 I' Y; d, {; g
}catch (FileNotFoundException e){
. _3 s: g, y& m+ V9 E: Q6 y
- @% C2 e" U# B7 s
continue;1 C) }' R0 e! b7 p" o" b7 w. }# i" w
+ S! L# H1 i% ?. ~/ e$ I4 L
2 h0 J2 A" B7 V8 k. f# c$ j# m4 u
}1 l: Y& f$ L* |/ z
catch (MalformedURLException e) {7 w G9 `- J: N) c, Q
, v- p+ ?3 a9 d# {5 ^ c& ~
System.out.println("无效的URL: " + urlString);
& v; }6 [( z ~- {; E: z
}catch(IOException e) {$ v) L& U7 e" K7 D# L. V5 a$ ?
e.printStackTrace();# H2 N6 i7 b6 R$ ] a4 k$ h
7 Z% i! L, ^( f! W
}, g, m3 u+ f$ W! @ |
Pattern pat =Pattern.compile("<divclass=\"popuserinfo\">(.+?)</td></tr></table>");7 A7 d8 |* K0 o* D
Matcher mat=pat.matcher(html_text);$ o$ G; X X. S0 G: k
0 K* Y9 G6 h9 y$ z% j
if(!mat.find()){
3 n. H& B7 I3 |5 j( X: l
continue;5 t" h, T u* A% I7 L5 b5 Z" E s
7 S% P7 m% d& K1 w4 A& f
}
while(mat.find()){( @& f! R; B9 O0 O8 Z ^9 w
Stringstrr=mat.group();. B% ~: F! z4 i) F
3 }; F: v5 r+ m% x, l9 D
System.out.println(strr);
% M) W+ {- V8 n
text+=strr+"\n";
( e2 x2 e, J" K3 N
}
}2 {- D. g) V7 ?
try{$ k' A* S- ^0 r+ a2 K
path=dirFile+"\\"+i+".txt";
Filefile = new File(path);5 q& m& y1 R9 A4 J
if(!file.exists()), d8 t+ J, j2 P! ]: |
file.createNewFile();6 w& b2 z0 f# n. ?2 E- A
fw=newFileWriter(file);9 K" E8 O) P+ A
fw.write(title+"\n");) Z. e/ V X3 ]& j, g
fw.write(text);- N5 G C& a2 D
}catch(IOException e){9 h, h9 D6 w' w. O2 X
System.out.println("输入输出异常");
: b/ M- [1 `3 }: R& ]; z
}finally{
if(fw!=null)
try{fw.close();}catch(IOException e){}
}
}
Dateendtime=new Date();7 g/ K3 ?0 H o- F4 l% n2 m1 J
longtime=endtime.getTime()-starttime.getTime();
System.out.println("用时:"+time+"ms");- w+ |& o) v) d5 a, X
}
} 附录4OtherDb.java 将其余信息写进数据库package huazhong; import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;import java.util.Date; public/ ]# d6 R, J% m) ?$ P: b
class OtherDb {
public, i% I: {8 P! V/ i
static# x7 }& w% {5 Q/ L% N. w8 k2 v
void main(String[] args) {; K/ r8 s4 V/ w6 `* m+ v
Date starttime=new Date();
int idMark=1;
int id2Mark=1;
int i,uid,ontm,tid,suid;
String name=null,title=null;
//StringmainUrl="http://diybbs.it168.com/";
Connection connection=null;2 F3 F. U% e l% \! o9 ~
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");' v+ Q/ ^) c1 f$ ?9 b$ P2 b2 Z
connection = DriverManager.getConnection(& K/ C z, y$ P
"jdbc:sqlserver://localhost:1433;DatabaseName=bbsdb",# ^. i, b- a- d5 g8 k* E( m. H
"sa", "123");
}catch (Exception e) {6 C' [" s2 _5 z) _! Y' M
System.out.println(e.getMessage());
}% @' I+ Y" Z; S- X% K2 ~4 s" q v; ?$ o/ M
String path="E:/HComment/";
File f = new File(path);# w2 k4 v- }/ J
File[] list = f.listFiles();
for(i=0;i<list.length;i++){* ^* t/ f7 ?3 I! z' g) T* ~. v
int flag1=0,flag2=0;
try{
FileReader reader = new FileReader(list);7 u/ T4 g; E% n
BufferedReader br = new BufferedReader(reader); 2 P1 t4 n, u+ n1 H( P1 v+ g
String string = null;& m% q! Q+ F/ b
title=br.readLine();
string=br.readLine();
if(string.indexOf("<")==-1){
continue;: o, ~+ v$ a5 z! J/ o6 w
}7 J5 \7 }( P" l6 V! H+ N
//
$ ]! @3 `. a9 o7 P2 p
name=string.substring(string.indexOf("_blank\">")+8,string.indexOf("</a>"));: E7 O( m) ?& ]" T: N. L* }
% H D/ h* q/ T3 O1 t9 @
//
try{
uid=Integer.parseInt(string.substring(string.indexOf("<dd>")+4,string.indexOf(" ")));
}catch (Exception e) {
8 u6 Z' f8 V J6 k3 X, u0 F2 G
continue;
& b& f0 R1 C, u7 o
7 T& L. t0 [3 I
}1 T; X/ ~0 |1 y1 Y, H& g
//
inta=string.lastIndexOf("小时"); h7 T0 ?4 a& A1 m' ?
int b=string.lastIndexOf(">",a);
try {
ontm=Integer.parseInt(string.substring(b+1,a).trim());- k+ F$ b2 \. J ^& ^9 i
} catch (Exception e) {+ Z1 U! ~5 T# y
continue;9 p8 p$ w8 b* e8 l w/ a% e9 r: |
}) ^. i8 X1 W* }. l
int c=string.indexOf("ptid");
int d=string.indexOf("&",c);3 C: ~8 S" b# Y6 v: @" M4 \
try {
tid=Integer.parseInt(string.substring(c+5,d));
} catch (Exception e) {
continue;2 v: T9 O+ s0 } P
}8 f! E8 b* J7 |7 {. s- h
try{1 m+ z; O1 f5 L3 j+ N
Statement stmt=connection.createStatement();) Q6 [+ V2 T" c) L/ Y; k
ResultSet res = stmt.executeQuery("select uid from BbsUser where uid="+uid);
if(res.next()){ h! \/ V, |. ^6 n
flag1=1;
}1 ^0 E8 x. h# q$ ]9 Z& K- h6 ]
if(flag2==0){
PreparedStatement pstmt=null;
String expr="insertinto BbsUser (id,uid,name,ontm) values (?,?,?,?)";; m l: l; \8 e% v
pstmt =connection.prepareStatement(expr); ! H+ y) s6 C) }' Z$ @1 w7 k
pstmt.setInt(1, idMark);, f% ~. N# Z- n" p
pstmt.setInt(2, uid);2 f6 x" _9 k& M4 r% `$ u. F
pstmt.setString(3, name);
pstmt.setInt(4, ontm);. Z* c6 W" h k) E$ ~! C
pstmt.executeUpdate();6 [, i; E3 P3 [
idMark++;, ?' w* t2 \. a. T- z
}
}catch (Exception e1) {
System.out.println(e1);
$ V. m/ {2 m% C' q' o4 |. S" z
}+ E) @# ~ P9 }9 U
while((string = br.readLine()) != null) {
+ M- @8 N* Q/ g8 N7 c
suid=Integer.parseInt(string.substring(string.indexOf("<dd>")+4,string.indexOf(" ")));8 j+ m5 {9 z5 c5 w5 j
name=string.substring(string.indexOf("_blank\">")+8,string.indexOf("</a>"));. F0 A' s0 o) o
//
) U% o6 g+ w9 v h8 D; d
//uid=Integer.parseInt(string.substring(string.indexOf("<dd>"+4),string.indexOf(" ")));0 y) p, B3 c0 P6 a
//0 C$ t3 r8 h+ ]$ l/ J f: J
/ \4 @" n. H+ v2 T
a=string.lastIndexOf("小时");
b=string.lastIndexOf(">",a);
ontm=Integer.parseInt(string.substring(b+1,a).trim());% L% h a8 t7 t5 J
c=string.indexOf("ptid");) ]) q8 t* E- l0 ` _. X$ }9 e
d=string.indexOf("&",c);
tid=Integer.parseInt(string.substring(c+5,d));
try {
PreparedStatement pstmt =null;
String expr="insert into Comment(id2,tid,uid,suid) values (?,?,?,?)";
pstmt =connection.prepareStatement(expr);3 b1 T3 O- e, |
pstmt.setInt(1, id2Mark);- ?% H8 ?: p1 \* G8 }
pstmt.setInt(2, tid);0 u W* F0 o6 J0 j" g1 H: Y
pstmt.setInt(3, uid);, X" J+ j( i) ^% |
pstmt.setInt(4, suid);" M A; n" M0 `% K8 F
id2Mark++;8 Z$ Q1 c2 e' c. u! W- g9 {% j
} catch (Exception e) {/ F8 s+ B! _: o
System.out.println(e);
}2 d8 \6 _! M7 O1 t2 a/ a" b
try{- Z8 I/ w+ u8 O' D3 r1 F5 G% j
Statement stmt=connection.createStatement();8 F# B0 Y; D( |( M
ResultSet res = stmt.executeQuery("select uid from BbsUser where uid="+suid);( c9 a" w; K7 p- R, l0 R
if(res.next()){
flag2=1;, a- ]/ Z6 z+ _& F5 O
}
if(flag2==0){9 C# a( {2 O1 x) ~
PreparedStatement pstmt=null;: ?" V( N# x0 H' t7 _( Y: v7 A
String expr="insert into BbsUser (id,uid,name,ontm) values (?,?,?,?)";9 R! C) J/ y$ s3 m1 f
pstmt =connection.prepareStatement(expr); " C4 x: a. y6 s* @: G9 Z# U5 I
pstmt.setInt(1, id2Mark);
pstmt.setInt(2, suid);6 O0 n, e( s' q2 b0 D
pstmt.setString(3, name);! }: k5 C2 `2 a! I
pstmt.setInt(4, ontm);
pstmt.executeUpdate();
idMark++;
}
}catch (Exception e1) {8 ~( J5 {4 ? Y, ~
System.out.println(e1); U& v) u; m- M6 a# [
}
}}catch(IOException e){
System.out.println("读取文件异常");4 P, Z, a( P) @
}1 G1 J2 v* V C; _. d1 K
}
try{
connection.close();4 p, D. V% W8 r( y" k. a. N
}catch (Exception e) {
System.out.println(e);$ Z' S8 I& ^3 l4 }8 l" E
}
Date endtime=new Date();8 ^' U0 Z) ] J @
long time=endtime.getTime()-starttime.getTime();
System.out.println("用时:"+time+"ms");
}}

| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |