首页 > 开发 > 综合 > 正文

C#实现web信息自动抓取

2024-07-21 02:18:29
字体:
来源:转载
供稿:网友
,欢迎访问网页设计爱好者web开发。c#实现web信息自动抓取

netbug--爬虫 v1.02开发笔记

背景



随着internet的普及,网络信息正以极高的速度增长,在这么多数据中找到自己需要的信息是一件很繁琐的事情,找到需要的信息后如何获取也是件麻烦的事。这就需要internet信息抓取程序来代替人工的操作。

所谓internet信息抓取程序,就是程序会按照用户的关键词或关键网站来收集相应的信息,并提供给用户想要的信息格式。



信息量的增加会带来信息网站发布人员工作量的剧增,为实现信息发布系统实现信息自

动发布、减少工作人员工作量、即时跟踪最新信息,就需要自动信息提供程序,因此internet信息抓取程序应运而生。



目标



实现自定义网站信息分类抓取,存入本地数据库、生成静态页面或其它用户定义的信息结构,并下载与信息相关的多媒体文件。



开发



l 目标站点结构分析

本步骤是准确抓取信息个关键。

首先要选择更新频率高的页面做为抓取地址,然后分析要抓取内容页面url特点。

然后分析要抓取信息页面的元素特性,比如标题位置,内容位置 等,得到定位标记点。

将以上信息写成自己的配置文件或存到数据库中。

每个网站都需要分析,写出单独的配置文件,供抓取程序使用。



l 信息提取

根据配置文件取得要抓取页面url,使用httpwebrequest类获取内容:

//获取http页面函数

public string get_http(string a_strurl,int timeout)

{

string strresult ;



try

{

httpwebrequest myreq = (httpwebrequest)httpwebrequest.create(a_strurl) ;

myreq.timeout = timeout;

httpwebresponse httpwresp = (httpwebresponse)myreq.getresponse();



stream mystream = httpwresp.getresponsestream () ;



streamreader sr = new streamreader(mystream , encoding.default);

stringbuilder strbuilder = new stringbuilder();

while (-1 != sr.peek())

{

strbuilder.append(sr.readline()+"/r/n");

}



strresult = strbuilder.tostring();

}

catch(exception exp)

{

strresult = "错误:" + exp.message ;

}



return strresult ;



}

获取页面内容后,分析页面中连接地址取到要抓取的url:

//处理页面标题和链接

public string sniffweburl( string urlstr,string blockb,string blocke )

{

string urlch1 = "";

string urlch2 = "";

int end_n1 = 0;

int end_nums = 0;

int end_nums1 = 0;

int end_nums2 = 0;

int end_nums3 = 0;

string reutstr = "";

string retitle = "";

string ret = "";

try

{

int pos01 = urlstr.indexof( "." );

int pos02 = urlstr.lastindexof( "/" );

if( pos01 < 0 )

{

return "";

}

if( pos02 < 0 )

{

return "";

}

int pos03 = urlstr.indexof( "/",pos01 );

if ( pos03 < 0 )

{

urlch1 = urlstr;

urlch2 = urlstr;

}

else

{

urlch1 = urlstr.substring( 0,pos03 );

urlch2 = urlstr.substring( 0,pos02 );

}



string tmpallstr = new publicfun().get_http( urlstr ,time1);



int pos1 = tmpallstr.indexof( blockb );

int pos2 = tmpallstr.indexof( blocke,pos1 + blockb.length );

if ( pos1>0 && pos2>0 && pos2>pos1 )

{

ret = tmpallstr.substring( pos1 + blockb.length,pos2 - pos1 - blockb.length );

ret = ret.substring( ret.indexof( "<" ));

while( ret.indexof( "<a" ) >= 0 )

{

ret = ret.substring( 0,ret.indexof( "<a" ) ) + "<a" + ret.substring( ret.indexof( "<a" ) + 2 );

}

while( ret.indexof( "</a" ) >=0 )

{

ret = ret.substring( 0,ret.indexof( "</a" ) ) + "</a" + ret.substring( ret.indexof( "</a" ) + 3 );

}

while( ret.indexof( "href=" ) >=0 )

{

ret = ret.substring( 0,ret.indexof( "href=" )) + "href=" + ret.substring( ret.indexof( "href=" ) + 5 );

}

while( ret.indexof( "href=" ) >=0 )

{

ret = ret.substring( 0,ret.indexof( "href=" )) + "href=" + ret.substring( ret.indexof( "href=" ) + 5 );

}

while( ret.indexof( "href='" ) >=0 )

{

ret = ret.substring( 0,ret.indexof( "href='" )) + "href=/"" + ret.substring( ret.indexof( "href='" ) + 6 );

}

}

tmpallstr = ret;

int begin_nums = tmpallstr.indexof( "href=" );



while ( begin_nums >= 0 )

{

string tmpstra = "";

string tmpstrb = tmpallstr.substring( begin_nums + 5,1 );

if ( tmpstrb == "/"" )

{

end_n1 = begin_nums + 6;

if ( ( end_n1 + 1 ) > tmpallstr.length )

{

return "";

}

tmpstra = tmpallstr.substring( begin_nums+6,1 );

}

else

{

end_n1 = begin_nums + 5;

tmpstra = tmpstrb;

}



if ( tmpstra == "#" )

{

tmpallstr = tmpallstr.substring( end_n1 );

begin_nums = tmpallstr.indexof( "href=" );

}

else

{

end_nums1 = tmpallstr.indexof( " ",end_n1 );

end_nums2 = tmpallstr.indexof( ">",end_n1 );

end_nums3 = tmpallstr.indexof( "</a",end_nums2 );



if ( ( end_nums3 >= 0 ) && ( end_nums2 >= 0 ) )

{

retitle = tmpallstr.substring( end_nums2 + 1,end_nums3 - end_nums2 - 1 );



if ( end_nums1 > end_nums2 )

{

end_nums = end_nums2;

}

else

{

if ( end_nums1 < 0 )

{

end_nums = end_nums2;

}

else

{

end_nums = end_nums1;

}

}

string str4 = tmpallstr.substring( end_nums-1, end_nums - end_nums + 1 );



if ( str4 =="/"" || str4 == "'" )

{

end_nums = end_nums - 1;

}

string stotalone = tmpallstr.substring( end_n1,end_nums - end_n1 );



if ( stotalone.indexof( "http://" ) <0 )

{

if ( stotalone.indexof( "/" ) == 0 )

{

stotalone = urlch1 + stotalone;

}

else

{

int linshiintnum = 0;

int flags = 0;

string urlchange = urlstr;;

while( stotalone.indexof( "../" ) >= 0 )

{

stotalone = stotalone.substring( stotalone.indexof( "../" ) + 3 );

linshiintnum = linshiintnum + 1;

flags = flags +1;

}

while( ( urlchange.lastindexof( "/" ) >= 0 ) && ( linshiintnum >= 0 ) )

{

urlchange = urlchange.substring( 0,urlchange.lastindexof( "/" ) );

linshiintnum = linshiintnum - 1;

}

if ( flags == 0 )

{

stotalone = urlch2 + "/" + stotalone;

}

else

{

stotalone = urlchange + "/" + stotalone;

}

}

}

reutstr = reutstr + new publicfun().removehtmlcode( retitle ) + stotalone;



tmpallstr = tmpallstr.substring( end_nums3 + 4 );

begin_nums = tmpallstr.indexof( "href=" );

}

else

{

begin_nums = -1;

}

}

}

return reutstr;

}

catch( exception e)

{

return "";

}

}



得到要抓取内容的url后,处理该页面:

//获取链接内容并分类处理

public string getwebcontent( string gatherurl,string suburl,string subtitle,string b_content,string e_content,string b_filter,string e_filter,string root )

{

string tmpallstr = "";

string dfstrb = "";

string dfstre = "";

string repicstr = "";//图片返回路径

string recontentstr = "";

string pichtml = "images"; //本地图片路径



string urlch1 ="";

string urlch2 ="";

int pos1 = gatherurl.indexof( "." );

int pos2 = gatherurl.lastindexof( "/" );

if( pos1 < 0 )

{

return "";

}

if( pos2 < 0 )

{

return "";

}

int pos3 = gatherurl.indexof( "/",pos1 );

if ( pos3 < 0 )

{

urlch1 = gatherurl;

urlch2 = gatherurl;

}

else

{

urlch1 = gatherurl.substring( 0,pos3 );

urlch2 = gatherurl.substring( 0,pos2 );

}



tmpallstr = new publicfun().get_http( suburl,time1 );

//取稿源

string docfromstr = "";

if ( dfstrb != "" && dfstre != "" )

{

if ( tmpallstr != "" )

{

int b_docf = tmpallstr.indexof( dfstrb );

if ( b_docf > 0 )

{

int e_docf = tmpallstr.indexof( dfstre,b_docf + dfstrb.length );

if ( e_docf > 0 && e_docf > b_docf && e_docf - b_docf < 20 )

{

docfromstr = tmpallstr.substring( b_docf + dfstrb.length, e_docf - b_docf - dfstrb.length );

}

}

}

}

//取内容

if ( tmpallstr != "" )

{

int begin_strnum = tmpallstr.indexof( b_content );

if ( begin_strnum < 0 )

{

return "";

}

int end_strnum = tmpallstr.indexof( e_content,begin_strnum + b_content.length );

if ( end_strnum < 0 )

{

return "";

}

string stotalsubm = "";

if ( end_strnum > begin_strnum )

{

stotalsubm = tmpallstr.substring ( begin_strnum,end_strnum - begin_strnum );

}



if ( stotalsubm == "" )

{

return "";

}

//过滤无用信息

int bfnum = stotalsubm.indexof( b_filter );

if ( bfnum > -1 )

{

int efnum = stotalsubm.indexof( e_filter,bfnum );

if ( efnum > -1 )

{

if ( efnum > bfnum )

{

stotalsubm = stotalsubm.substring( 0,bfnum ) + stotalsubm.substring( efnum + e_filter.length );

}

}

}

//格式化图片标记



while( stotalsubm.indexof( "src=" ) >= 0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( "src=" ) ) + "src=" + stotalsubm.substring( stotalsubm.indexof( "src=" ) + 4 );

}

while( stotalsubm.indexof( "src=" ) >= 0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( "src=" ) ) + "src=" + stotalsubm.substring( stotalsubm.indexof( "src=" ) + 4 );

}

while( stotalsubm.indexof( "src='" ) >= 0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( "src='" ) ) + "src=/"" + stotalsubm.substring( stotalsubm.indexof( "src='" ) + 5 );

}



//取图片地址

int end_n12 = 0;

int end_nums2 = 0;

int begin_nums2 = stotalsubm.indexof( "src=" );

while( begin_nums2 >= 0 )

{

string tmpstr = stotalsubm.substring( begin_nums2 + 4,1 );

if ( tmpstr == "/"" )

{

end_n12 = begin_nums2 + 5;

}

else

{

end_n12 = begin_nums2 + 4;

}

int end_nums2a = stotalsubm.indexof( " ",end_n12 );

int end_nums2b = stotalsubm.indexof( ">",end_n12 );

if ( end_nums2b < 0 )

{

break;

}

if ( end_nums2a > end_nums2b )

{

end_nums2 = end_nums2b;

}

else

{

if (end_nums2a<0)

{

end_nums2 = end_nums2b;

}

else

{

end_nums2 = end_nums2a;

}

}

tmpstr = stotalsubm.substring( end_nums2-1,1 );

if ( tmpstr == "/"" || tmpstr == "'" )

{

end_nums2 = end_nums2 - 1;

}

string tmppicstr = stotalsubm.substring( end_n12,end_nums2 - end_n12 );



if ( tmppicstr.indexof( "http://" ) < 0 )

{

if ( tmppicstr.indexof( "/" ) == 0 )

{

tmppicstr = urlch1 + tmppicstr;

}

else

{

int linshiintnum = 0;

int flags = 0;

string urlchange = suburl;

while( tmppicstr.indexof( "../" ) >= 0 )

{

tmppicstr = tmppicstr.substring( tmppicstr.indexof("../") + 3 );

linshiintnum = linshiintnum + 1;

flags = flags + 1;

}

while( ( urlchange.lastindexof( "/" ) >= 0 ) && ( linshiintnum >= 0 ) )

{

urlchange = urlchange.substring( 0,urlchange.lastindexof( "/" ) );

linshiintnum = linshiintnum - 1;

}

if ( flags == 0 )

{

tmppicstr = urlch2 + "/" + tmppicstr;

}

else

{

tmppicstr = urlchange + "/" + tmppicstr;

}

}

}

//tmppicstr = tmppicstr.tolower();

string tmppicstrtmp = tmppicstr.tolower();

//if ( tmppicstr.indexof( ".jpg" ) > 0 || tmppicstr.indexof( ".gif" ) > 0 || tmppicstr.indexof( ".bmp" ) > 0 )

if ( tmppicstrtmp.indexof( ".jpg" ) > 0 || tmppicstrtmp.indexof( ".gif" ) > 0 || tmppicstrtmp.indexof( ".bmp" ) > 0 )

{

repicstr = repicstr + "||" + tmppicstr ;



int flagn2 = tmppicstr.lastindexof( "/" );

string filen2 = pichtml + tmppicstr.substring( flagn2 );

stotalsubm = stotalsubm.substring( 0,end_nums2 ) + ">******" + filen2 + "******<" + stotalsubm.substring( end_nums2 );



begin_nums2 = stotalsubm.indexof( "src=", end_nums2 + filen2.length + 22 );

}

else

{

begin_nums2 = stotalsubm.indexof( "src=", end_nums2 + 4 );

}

}

if ( repicstr.length > 2 )

repicstr = repicstr.substring(2);



//内容处理 格式化关键标记

while( stotalsubm.indexof( "<p" ) >= 0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( "<p" ) ) + "|****|<" + stotalsubm.substring( stotalsubm.indexof( "<p" ) + 2 );

}

while( stotalsubm.indexof( "<p" ) >= 0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( "<p" ) ) + "|****|<" + stotalsubm.substring( stotalsubm.indexof( "<p" ) + 2 );

}

while( stotalsubm.indexof( "</p" ) >= 0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( "</p" ) ) + "|****|<" + stotalsubm.substring( stotalsubm.indexof( "</p" ) + 3 );

}

while( stotalsubm.indexof( "</p" ) >= 0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( "</p" ) ) + "|****|<" + stotalsubm.substring( stotalsubm.indexof( "</p" ) + 3 );

}

while( stotalsubm.indexof( "<br" ) >=0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( "<br" ) ) + "+****+<" + stotalsubm.substring( stotalsubm.indexof( "<br" ) + 3 );

}

while( stotalsubm.indexof( "<br" ) >= 0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( "<br" ) ) + "+****+<" + stotalsubm.substring( stotalsubm.indexof( "<br" ) + 3 );

}

while( stotalsubm.indexof( "<br" ) >= 0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( "<br" ) ) + "+****+<" + stotalsubm.substring( stotalsubm.indexof( "<br" ) + 3 );

}

while( stotalsubm.indexof( "<br" ) >= 0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( "<br" ) ) + "+****+<" + stotalsubm.substring( stotalsubm.indexof( "<br" ) + 3 );

}



//去除html标记

int linshiint1 = stotalsubm.indexof( "<" );

int linshiint2 = stotalsubm.indexof( ">" );



if ( linshiint2 < linshiint1 )

{

stotalsubm = stotalsubm.substring( linshiint2 + 1 );

}

int linshiint11 = stotalsubm.lastindexof( "<" );

int linshiint12 = stotalsubm.lastindexof( ">" );

if ( linshiint12 < linshiint11 )

{

stotalsubm = stotalsubm.substring( 0,linshiint12 + 1 );

}

linshiint1 = stotalsubm.indexof( "<" );

while ( linshiint1 >= 0 )

{

linshiint2 = stotalsubm.indexof( ">",linshiint1 );

if ( linshiint2 >= 0 )

{

stotalsubm = stotalsubm.substring( 0,linshiint1 ) + stotalsubm.substring( linshiint2 + 1 );

}

else

{

stotalsubm = stotalsubm.substring( 0,linshiint1 );

}

linshiint1 = stotalsubm.indexof("<");

}



//还原关键标记

int linshiint3 = 0;

int linshiint4 = 0;



while( stotalsubm.indexof( "+****+" ) >= 0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( "+****+" ) ) + "<br>/n" + stotalsubm.substring( stotalsubm.indexof( "+****+" ) + 9 );

}

while( stotalsubm.indexof( "|****|" ) >= 0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( "|****|" ) ) + "<br>/n" + stotalsubm.substring( stotalsubm.indexof( "|****|" ) + 9 );

}

while( stotalsubm.indexof( "******" ) >= 0 )

{

linshiint3 = stotalsubm.indexof( "******" ) + 9;

linshiint4 = stotalsubm.indexof( "******",linshiint3 );

if ( linshiint4 >= 0 )

{

int tmppos = stotalsubm.indexof( "******" );

string tmpstr1 = stotalsubm.substring( 0,tmppos );

string tmpstr2 = stotalsubm.substring( linshiint3,linshiint4 - linshiint3 );

string tmpstr3 = stotalsubm.substring( linshiint4 + 9 );

stotalsubm = tmpstr1 + "<img src=" + tmpstr2 + ">" + tmpstr3;

}

else

{

break;

}

}

//去除内容中的标题

if ( stotalsubm.indexof( subtitle ) >= 0 )

{

stotalsubm = stotalsubm.substring( 0,stotalsubm.indexof( subtitle ) ) + stotalsubm.substring( stotalsubm.indexof( subtitle ) + subtitle.length );

}

recontentstr = stotalsubm;

//调用下载图片功能

//下载图片到指定目录

string[] img_url = new publicfun().split( repicstr,"||" );

for ( int i=0;i<img_url.length;i++ )

{

if ( img_url[i] != "" )

{

new publicfun().get_img( img_url[i],10000,root + "//images//" + img_url[i].substring( img_url[i].lastindexof("/")+1 ) );

}

}

}

return recontentstr;



}

以上方法返回要取得的信息,包括标题内容,图片地址等。



下载页面中图片:

//下载图片

public void get_img(string a_strurl,int timeout,string filepath)

{

try

{

httpwebrequest myreq = (httpwebrequest)httpwebrequest.create(a_strurl) ;

myreq.timeout = timeout;

httpwebresponse httpwresp = (httpwebresponse)myreq.getresponse();

stream mystream = httpwresp.getresponsestream () ;

bitmap map = new bitmap( mystream );

picturebox picb = new picturebox();

picb.image = (image)map;

string path = filepath.substring( 0,filepath.lastindexof( "//" ) );

if (!directory.exists(path))

{

createdir( path );

}

picb.image.save(filepath);

}

catch(exception exp)

{

string ss = exp.message;

writelog( filepath.substring(0,filepath.lastindexof("//")) + "//error.log",a_strurl + "--" + ss + "/r/n");

}

}

l 保存文件或入库

上面取得的信息可以按自己的要求保存。



****设计的时候没有使用url按层次循环抓取,这样定义抓取url效率更高,速度更快。





测试程序下载:http://bjfile.focus.cn/file/15379/netbugv102.rar



如有建议请发送email:[email protected] 或msn [email protected]



注:此版本只提供静态文件存储功能,不提供数据库接口,不提供自定义网站功能。

本程序运行需要先安装.net 框架1.1。







发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表