小偷程序之网页分块筛选

                                     ASP.Net、C#实现网页小偷程序

asp.net、c#实现网页小偷程序
      在asp中实现小偷是使用xmlhttp对象,最近在学习.net时看到了webclient类,于是将过去用来做小偷的程序改了一下,使用asp.net、c#来实现,程序写的比较简单,目的是为了起到抛砖引玉的作者,希望能与各位一起探讨,使之更加完善,下一步我将使之实现根据设置可以获取网页中指定的内容。以下是程序部分,包括在web页中的asp.net的源程序和c#中的源程序。

asp.net (getwebcontent.aspx)

<%@ page language="c#" %>
<%@ import namespace="system.net" %>
<%@ import namespace="system.text" %>
<script runat=server>
   //***********************************************************
   //*             
   //*    使用asp.net实现网站小偷的程序    
   //*       written by 笑笑 2005-12-11      
   //*       网址:http://blog.hnce.net      
   //*       email:hedongyang@gmail.com qq:5364083   
   //*              
   //***********************************************************
void page_load(object sender , eventargs e)
{
   string strurl="http://blog.hnce.net";    //欲获取的网页地址
  
   webclient mywebclient=new webclient();    //创建webclient实例mywebclient
  
   //获取或设置用于对向 internet 资源的请求进行身份验证的网络凭据。
   mywebclient.credentials=credentialcache.defaultcredentials;
  
   //从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)
   byte[] pagedata=mywebclient.downloaddata(strurl);
  
   //以下两句每次只要使用一条即可,功能是一样是用来转换字符集,根据获取网站页面的字符编码选择
   //string result=encoding.default.getstring(pagedata);       
   //如果获取网站页面采用的是gb2312,则使用这句
   string result=encoding.utf8.getstring(pagedata);
   //如果获取网站页面采用的是utf-8,则使用这句
   //因为我的博客使用了utf-8编码,所以在这里我使用这句
   response.write(result);   //在web页中显示获取的内容
}
</script>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
</body>
</html>

c# (getwebcontent.cs)

/*
*********************************************************
*      
*  使用c#实现网站小偷的程序  
*       written by 笑笑 2005-12-11  
*       网址:http://blog.hnce.net  
*       email:hedongyang@gmail.com qq:5364083
*      
*********************************************************
*/
using system;
using system.net;
using system.text;

class getwebcontent
{
 public static void main()
 {          
     try
     {

  webclient mywebclient = new webclient();

  mywebclient.credentials = credentialcache.defaultcredentials;

  byte[] pagedata = mywebclient.downloaddata("http://blog.hnce.net");
  string pagehtml = encoding.utf8.getstring(pagedata);
  console.writeline(pagehtml);

     }
     catch (webexception webex)
     {
  console.write(webex.tostring());
     }
 }
}

下面为使用正则实现筛选 

privatestring ClHtml(string html)
    {
        
string str ="";
        
string strtemp ="";
        
string[] s;
        
int n =0;
        Regex r
=new Regex(@"weathertitle([\w\W]*?)text/javascript");
        MatchCollection ms
= r.Matches(html);

        
foreach (Match m in ms)
        {

            
try
            {
                str
= m.Groups[1].Value;

                str
="<div class=\""+ str;
                str
= str.Replace("<script type='", "");

            }
            
catch { }
        }


        
//Regex rr = new Regex(@"title([\w\W]*?)</td>");
        
//MatchCollection mss = rr.Matches(str);
        
//int dd = 0;
        
//int tell = 0;
        
//int tell1 = 0;

        
//string nametxt = "";
        
//string telltxt = "";
        
//foreach (Match mm in mss)
        
//{

        
//    try
        
//    {
        
//        strtemp = mm.Groups[1].Value;
        
//        strtemp = strtemp.Replace("\t", "").Replace("\n", "");

        
//        dd = strtemp.LastIndexOf("\">");
        
//        nametxt = strtemp.Substring(0, dd).Replace("=\"", "");
        
//        tell = strtemp.LastIndexOf("电话:");
        
//        tell1 = strtemp.LastIndexOf("</span>");
        
//        telltxt = strtemp.Substring(tell, tell1 - tell).Replace("电话:", "");
        
//        no = no + 1;
        
//        label2.Text = no.ToString() + "条";

        
//        textBox2.Text = textBox2.Text + nametxt + " " + telltxt + "\r\n";
        
//    }
        
//    catch { }
        
//}
        
//if (ms.Count == 0)
        
//{
        
//    n = 1;
        
//}
        return n;
    }


posted @ 2012-03-20 00:15  酒沉吟  阅读(320)  评论(1编辑  收藏  举报