日期:2014-05-17  浏览次数:20396 次

爬取页面需要登陆才可爬取,这种怎么解决
如题,就是如果要爬取某个页面,但它必须要你在它的登陆页面,登陆后,内容才可以显示出来,请问这种的是怎么解决,谢了。

------解决方案--------------------
我的做法是用WebBrowser模拟用户登录,如果带验证码的,可以用验证码识别算法,或者弹出一个对话框输入验证码,然后再跳转到要爬取的页面
------解决方案--------------------
这个也就是模拟登录,没有验证码的好办.
用抓包工具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;