zoukankan      html  css  js  c++  java
  • 【转】小谈C#.NET下的爬虫(蜘蛛)技术 Frida

     

    爬虫,又称蜘蛛,是从别的网站抓取资源的一种方法,C#.NET使用爬虫的方法如下:

     

    protected string GetPageHtml(string url)
    
        {
    
            string pageinfo;
    
            try
    
            {
    
                WebRequest myreq = WebRequest.Create(url);
    
                WebResponse myrep = myreq.GetResponse();
    
                StreamReader reader = new StreamReader(myrep.GetResponseStream(), Encoding.GetEncoding("gb2312"));
    
                pageinfo = reader.ReadToEnd();
    
            }
    
            catch
    
            {
    
                pageinfo = "";
    
            }
    
            return pageinfo;
    
    }
    

    按上述方法就可以在程序中获取某URL的页面源文件。

     

    但是有些网站屏蔽了爬虫,那就需要模拟浏览器获取的方法来进行,具体代码如下:

    protected string GetPageHtml(string url)
    
        {
    
            string pageinfo;
    
            try
    
            {
    
                HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
    
                myReq.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
    
                myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
    
                HttpWebResponse myRep = (HttpWebResponse)myReq.GetResponse();
    
                Stream myStream = myRep.GetResponseStream();
    
                StreamReader sr = new StreamReader(myStream, Encoding.Default);
    
                pageinfo = sr.ReadToEnd().ToString();
    
            }
    
            catch
    
            {
    
                pageinfo = "";
    
            }
    
            return pageinfo;
    
    }
    
    

    转帖请注明http://www.iamheyi.com

  • 相关阅读:
    继续致歉
    向大家致歉!!
    访问速度调查
    致歉
    [功能改进]通过Blog客户端直接发随笔至网站分类
    带宽升级公告
    创业团队的类型
    [讨论]基于.NET的开源论坛软件的选择
    调整一下工作的节奏
    公告
  • 原文地址:https://www.cnblogs.com/luckjun/p/2068488.html
Copyright © 2011-2022 走看看