六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 149|回复: 0

利用HttpWebRequest自动抓取

[复制链接]

升级  10%

66

主题

66

主题

66

主题

举人

Rank: 3Rank: 3

积分
230
 楼主| 发表于 2013-2-7 20:32:59 | 显示全部楼层 |阅读模式
1、利用httpwatch找到网站入口及参数:(详情略,请百度httpwatch的使用方法)
2、利用httpwebRequest将51拉查看密码Post到网站入口,登陆成功后再进入你想要进入的页面抓取页面 
<div class="cnblogs_code"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->        /// <summary>
        
/// 某日或者叫时间段,51la里组员ID里——搜索引擎内——的数据。
        
/// </summary>
        
/// <param name="strStaticId"></param>
        
/// <param name="LookPass"></param>
        
/// <param name="strBeginDate"></param>
        
/// <param name="strEndDate"></param>
        
/// <returns></returns>
        public string FiveLaSeo(string strStaticId, string LookPass,string strBeginDate,string strEndDate)
        {
            CookieContainer Cc 
= new CookieContainer();

            ASCIIEncoding encoding 
= new ASCIIEncoding();
            
string postData = "id=" + strStaticId;
            postData 
+= ("&lookpass=" + LookPass);
            postData 
+= "&t=chalogin";

            
//将提交的字符串数据转换成字节数组
            byte[] data = encoding.GetBytes(postData);

            
// 设置提交的相关参数   
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.51.la/report/0_help.asp");
            myRequest.Method 
= "POST";
            myRequest.ContentType 
= "application/x-www-form-urlencoded";
            myRequest.ContentLength 
= data.Length;
            
//cookie的容器一定要加
            myRequest.CookieContainer = Cc;

            
// 提交请求数据  
            Stream newStream = myRequest.GetRequestStream();            
            newStream.Write(data, 
0, data.Length);
            newStream.Close();

            
// 接收返回的页面  
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader 
= new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
            
string content = reader.ReadToEnd();

            
//进去后打开特定页面的参数设置
            myRequest = (HttpWebRequest)WebRequest.Create("http://www.51.la/report/3_SE.asp?id=" + strStaticId + "&d1=" + strBeginDate + "&d2=" + strEndDate);
            myRequest.Method 
= "GET";
            myRequest.KeepAlive 
= false;
            myRequest.CookieContainer 
= Cc;

            
//接收返回的特定页面
            myResponse = (HttpWebResponse)myRequest.GetResponse();
            newStream 
= myResponse.GetResponseStream();
            reader 
= new StreamReader(myResponse.GetResponseStream(),Encoding.Default);
            content 
= reader.ReadToEnd();
            
return content;
        }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表