所谓的数据采集程序也就是网页小偷程序(大家别骂我哦),写完了来这里发点东西,希望大家有何高见共同研究.
1.在下载数据的开始,有些网站是要登录了才能看到相应的数据,这个就需要我们发送登录用户名和密码了,但我是登录了,但他服务器也不是垃圾,在他那里重定向了,共产生了2个session,这第2个session我就不知道如何捕抓.于是我就投机^-^,用软件将session捕抓下来了1个叫ethereal的软件,用以下代码加入到http请求的头部
webclient mywebclient = new webclient();
string sessionkey=textbox78.text;
string refererurl=textbox77.text;
mywebclient.headers.clear();
mywebclient.headers.add("cookie",sessionkey);
mywebclient.headers.add("referer", refererurl);
mywebclient.headers.add("user-agent", "mozilla/5.0 (x11; u; linux i686; en-us; rv:1.5) gecko/20031107 debian/1.5-3");
这样就欺骗了服务器了,哈哈
2.第二部就是代码下载
byte[] mydatabuffer = mywebclient.downloaddata(remoteuri);
download = encoding.default.getstring(mydatabuffer);
3.第3部就是数据的匹配了,我是将流读取到数据里,然后用indexof得到2个关键字段的位置,然后用substring取出来的,我知道这很笨,但用正则表达式难啊(谁会的指点我下),匹配完了得到的字符串我就用以下的函数去掉了html代码:
private string striphtml(string strhtml)
{
string [] aryreg ={
@"<script[^>]*?>.*?</script>",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr][^\7])*?\7\w+).{0})\s)*?(\/\s*)?>",
@"([\r\n])[\s]+",
@"&(quot#34);",
@"&(amp#38);",
@"&(lt#60);",
@"&(gt#62);",
@"&(nbsp#160);",
@"&(iexcl#161);",
@"&(cent#162);",
@"&(pound#163);",
@"&(copy#169);",
@"&#(\d+);",
@"-->",
@"<!--.*\n"
};
string [] aryrep = {
"",
"",
"",
"\"",
"&",
"<",
">",
" ",
"\xa1",//chr(161),
"\xa2",//chr(162),
"\xa3",//chr(163),
"\xa9",//chr(169),
"",
"\r\n",
""
};
string newreg =aryreg[0];
string stroutput=strhtml;
for(int i = 0;i<aryreg.length;i++)
{
regex regex = new regex(aryreg[i],regexoptions.ignorecase );
stroutput = regex.replace(stroutput,aryrep[i]);
}
stroutput.replace("<","");
stroutput.replace(">","");
stroutput.replace("\r\n","");
return stroutput;
}
4.到了后面就是入库了,这个大家都懂了吧.但是我还有点问题就是,在我写数据的时候,出了exception,说我的字段太长了,不能写进到数据库,我用的是access,我试验下用sql吧.
5.大家有什么好的建议给我留个言赛.共同进步嘛.
出处:http://jetadv.cnblogs.com/archive/2006/02/18/333213.html
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器