C#登录后携带cookie爬取数据

前一段时间,公司以前的一个数据采集任务突然之间采集下来的数据都是0了,也就是未登录状态能够获取到的数据,于是猜想肯定是网站的服务升级了,升级了数据接口的逻辑,于是便开始解决此问题。

此采集程序是由.net core开发,采用Quartz定时任务定时采集数据。

下面是解决方法:

1.首先从登录url拿到cookie,然后保存至内存中

2.打开需要爬取的网页时,将拿到的cookie放到打开网页的request中

3.在第2步中ContentType 和UserAgent 需要与登录获取cookie一致

复制代码
        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <param name="url">登录url,例如http://www.website.com/user/doLogin</param>
        /// <param name="params">登录参数,例如username=username&pwd=pwd&forever=1</param>
        /// <returns></returns>
        public static CookieCollection GetCookieCollection(string url, string params)
        {
            CookieContainer cc = new CookieContainer();
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(params);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            request.UserAgent = "Chrome/87.0.4280.66"; 

            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            request.CookieContainer = cc;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            CookieCollection cookieCollection = response.Cookies;
            DateTime dt = DateTime.Now.AddMinutes(365 * 24 * 60);//为cookie添加过期时间
            for (int i = 0; i < cookieCollection.Count; i++)
            {
                cookieCollection[i].Expires = dt;
            }

            return cookieCollection;
        }

        /// <summary>
        /// 获取网页代码
        /// </summary>
        /// <param name="url">需要爬取的数据所在网页url</param>
        /// <param name="cookieCollection">GetCookieCollection方法获取到的cookie</param>
        /// <returns></returns>
        public static string GetWebContent(string url, CookieCollection cookieCollection)                     {
            CookieContainer cc = new CookieContainer();
            cc.Add(cookieCollection);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.CookieContainer = cc;
            request.ContentType = "application/x-www-form-urlencoded";//与登录一致
            request.UserAgent = "Chrome/87.0.4280.66"; //与登录一致
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream stream = response.GetResponseStream();
            string webContent = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
            return webContent;
        }
相关推荐
吃我一窝jio18 分钟前
Python与Go线程模型深度对比
java·开发语言·python
eggwyw42 分钟前
C++ union 的一种妙用法
开发语言·c++
何以解忧唯有撸码3 小时前
开源】当桌面图标开始打台球:我用 WinForms 做了一桌“真的”台球
c#·winform·自定义控件·gdi
starzy19903 小时前
深入理解Scala隐式函数:自动类型转换
开发语言·后端·scala
布莱克6055 小时前
Swift 循环与控制转移详解:从 for-in 到标签跳转
开发语言·ssh·swift·循环
敲上瘾6 小时前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
联犀6 小时前
ai时代复杂项目的golang项目如何更好的与ai共舞:monorepo+gozero
开发语言·人工智能·golang
lcj25118 小时前
[C++] C++11
开发语言·c++·笔记
Evand J10 小时前
【MATLAB例程】基于IMM-CKF的AOA+测距融合的二维移动目标跟踪定位
开发语言·matlab·目标跟踪
JianZhen✓11 小时前
IM 聊天列表为什么总“消息乱跳“:有序插入、时间轴派生、本地截断、已读回执
开发语言·前端·php