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;
        }
相关推荐
是立不是利14 分钟前
大文件导致请求被拒的连锁反应
开发语言·前端·javascript
野生技术架构师32 分钟前
1200 道 JAVA 面试题(各大企业常见面试题及答案)
java·开发语言
鱼听禅42 分钟前
C# 学习笔记-使用 类型化客户端搭建HTTP客户端服务
学习·http·c#·工厂方法模式
“AI国潮设计-小江”42 分钟前
【Python/SDXL实战】潮汕国潮IP视觉落地:普宁美食猫IP & 创意甜品设计(附ComfyUI工作流思路)
开发语言·人工智能·python·prompt·aigc
Yyyyyy~1 小时前
【java】类和对象
java·开发语言
草莓熊Lotso1 小时前
【Redis 进阶】主从复制深度解析:从配置落地到 PSYNC 同步原理
linux·开发语言·网络·数据库·redis·缓存·php
Joy T1 小时前
Spring AI 2.0 进阶入门:Workflow、Routing、Task State 与可控 Agent
开发语言·人工智能·workflow·routing·springai·orchestrator·evaluator
重生之小比特2 小时前
【Java SE】抽象类和接口
java·开发语言
传奇开心果编程2 小时前
【Rust入门知识点学与练】第33课:异步编程入门(async/await)强化
开发语言·学习·rust
传奇开心果编程2 小时前
【Rust入门知识点学与练】第34课:所有权与借用
开发语言·学习·rust