爬取页面需要登陆才可爬取,这种怎么解决

cainiao13579 2013-01-21 08:58:57
如题,就是如果要爬取某个页面,但它必须要你在它的登陆页面,登陆后,内容才可以显示出来,请问这种的是怎么解决,谢了。
...全文
1243 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
HeraLu 2013-01-25
  • 打赏
  • 举报
回复
要看有没有验证码,有验证码比较麻烦,没有验证码的话,用WebBrowser控件,添加引用Microsoft.mshtml

using mshtml;
namespace Parser
{
    class Spider:Form
    {
        private void Spider_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("登录界面的URL");
        }
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (e.Url.ToString() != webBrowser1.Url.ToString())
                return;
            if (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
                return;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string htmlDoc = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;
            IHTMLElementCollection eCollection = htmlDoc.all;
            foreach (IHTMLElement element in eCollection)
            {
                //假设该页面用户名密码的输入框都是放在input中的
                if (element.tagName.ToLower() == "input")
                {
                    //假设用户名输入框input的name属性值是username,同理密码输入框属性值是password。即<input ... name=username ...>和<input ... name=password...>,不一定非要用name属性,id什么也可以,只要能区分就可以
                    object attrtext = element.getAttribute("name", 0);
                    if (attrtext != null)
                    {
                        if (attrtext.ToString() == "username")
                            element.setAttribute("value", "自己赋值");
                        if (attrtext.ToString() == "password")
                            element.setAttribute("value", "自己赋值");
                    }
                    //假设网页上的提交按钮源代码是<input type='submit' name='submit' value=' 提 交 '>    
                    object attr = element.getAttribute("type", 0);
                    if (attr != null)
                    {
                        if (attr.ToString() == "submit")
                        {
                            element.click();
                        }
                    }
                }
            }
        }
    }
}
这就完成模拟登录啦! 登录完之后再string htmlDoc = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;就爬取到了登录后的源代码了!
  • 打赏
  • 举报
回复
模拟用户登录 使用HttpWebRequest发送用户信息
wansai00 2013-01-25
  • 打赏
  • 举报
回复
手工登陆 拿到当前Cookie 在你的程序里 抓页面的时候 把Cookie一并传过去
枫c_2012 2013-01-25
  • 打赏
  • 举报
回复
看到很不错、、不错、学学
txiangsun 2013-01-25
  • 打赏
  • 举报
回复
关注下
yoyo_ 2013-01-25
  • 打赏
  • 举报
回复
这个也就是模拟登录,没有验证码的好办. 用抓包工具wireshark看下那个网站登录是post到哪的,需要带哪些参数,登录成功后再请求需要的数据页,最近也有做过类似的功能,代码你可以参考下.

         public HttpWebResponse PostData(string strURL, string strArgs, string strReferer, string code, string method, CookieContainer cookieContainer)
        {
            return PostData(strURL, strArgs, strReferer, code, method, string.Empty,cookieContainer);
        }
        public HttpWebResponse PostData(string strURL, string strArgs, string strReferer, string code, string method, string contentType, CookieContainer cookieContainer)
        {
            try
            {
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
                myHttpWebRequest.AllowAutoRedirect = true;
                myHttpWebRequest.KeepAlive = true;
                myHttpWebRequest.Accept = "application/json, text/javascript, */*";
                myHttpWebRequest.Referer = strReferer;

                myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.96 Safari/537.4";
                if (string.IsNullOrEmpty(contentType))
                {
                    myHttpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                }
                else
                {
                    myHttpWebRequest.ContentType = "contentType";
                }

                myHttpWebRequest.Method = method;
                myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate,sdch");

                if (cookieContainer == null)
                {
                    cookieContainer = new CookieContainer();
                }
                myHttpWebRequest.CookieContainer = cookieContainer;
                byte[] postData = Encoding.GetEncoding(code).GetBytes(strArgs);
                myHttpWebRequest.ContentLength = postData.Length;
                System.IO.Stream PostStream = myHttpWebRequest.GetRequestStream();
                PostStream.Write(postData, 0, postData.Length);
                PostStream.Close();

                HttpWebResponse response = null;
                response = (HttpWebResponse)myHttpWebRequest.GetResponse();
                return response;
            }
            catch (Exception ex)
            {
                string s = "出错了:" + ex.Message;
                return null;
            }
        }

          private void login_ajax()
        {
            string username = Request.Form["username"];
            //登录地址
            string LOGIN_URL = "http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
            //登录请求来源地址
            string LOGIN_REFERER = "http://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm-login&lang=zh_CN";
            CookieContainer cookie = new CookieContainer();
            if (Session["login_verify_code"] != null)
            {
                cookie = Session["login_verify_code"] as CookieContainer;
            }
            string is_update = Request.QueryString["is_update"] ?? "";
            string pwd1 = Request.Form["pwd1"];
            string pwd2 = Request.Form["pwd2"];
            string imgcode = Request.Form["imgcode"];
            string register = Request.Form["register"];
            string f = Request.Form["f"];
            //拼接请求参数
            string strArgs = "&username=" + username;
            strArgs += "&pwd1=" + pwd1;
            strArgs += "&pwd2=" + pwd2;
            strArgs += "&imgcode=" + imgcode;
            strArgs += "&f="+f;
            HttpWebResponse http_response = pt.PostData(LOGIN_URL, strArgs, LOGIN_REFERER, CODE, MOTHED, cookie);
            StreamReader reader = new StreamReader(http_response.GetResponseStream(), Encoding.Default);
            content = reader.ReadToEnd();

            Response.Write(content);
            reader.Close();
            http_response.Close();
            Session["username"]=username;
            Session["pwd"] = pwd1;
            Session["login_wx"] = cookie;
            
            //此处是登录成功后要取的网页
            string temp_url = "http://mp.weixin.qq.com/cgi-bin/userinfopage?t=wxm-setting&lang=zh_CN";
            HttpWebResponse http_response_get = pt.GetResponseByGet(temp_url, cookie);

            StreamReader reader1 = new StreamReader(http_response_get.GetResponseStream(), Encoding.Default);
            string content1 = reader1.ReadToEnd();
          
        }

dalmeeme 2013-01-24
  • 打赏
  • 举报
回复
这个最一般了,先用post方式请求登录,登录成功后再请求数据页,可能需要携带登录成功后的cookies请求数据页。具体自己用抓包工具看下。
cainiao13579 2013-01-24
  • 打赏
  • 举报
回复
没有人么?。。。。。。
翔教授 2013-01-21
  • 打赏
  • 举报
回复
我的做法是用WebBrowser模拟用户登录,如果带验证码的,可以用验证码识别算法,或者弹出一个对话框输入验证码,然后再跳转到要爬取的页面
cainiao13579 2013-01-21
  • 打赏
  • 举报
回复
引用 1 楼 zsyok 的回复:
过滤下蜘蛛的IP让它进呗~
就是想爬去某个页面,但是但是他要你登陆了才可以爬取
zsyok 2013-01-21
  • 打赏
  • 举报
回复
过滤下蜘蛛的IP让它进呗~
cainiao13579 2013-01-21
  • 打赏
  • 举报
回复
引用 4 楼 xiangjiaoshou 的回复:
我的做法是用WebBrowser模拟用户登录,如果带验证码的,可以用验证码识别算法,或者弹出一个对话框输入验证码,然后再跳转到要爬取的页面
不带验证吗?但就是要登陆下才可以看到内容。

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧