当前位置: 首页 > news >正文

网络编程 —— Http使用httpClient实现页面爬虫

先去找类型的a标签 取出图片所在网址 取出https://desk.3gbizhi.com/deskMV/438.html

搭建Form界面

Http类 

public static HttpClient Client { get; }
static Http()
{HttpClientHandler handler = new HttpClientHandler();//处理消息对象//ServerCertificateCustomValidationCallback  是否开启免验证策略,有的网站不安全,//浏览器阻止你访问,需要把验证忽略掉handler.ServerCertificateCustomValidationCallback = (message, cart, chain, error) => { return true; };Client = new HttpClient(handler);//请求对象}

图片所在页面网址的正则

Regex imgHtml = new Regex(@"<a href=""(https://[a-zA-Z0-9/\.]+\.html)"" class=""[a-zA-Z0-9]* imgw"" target=""_blank"">" );
//< a href = "https://pic.3gbizhi.com/uploadmark/20231006/c54bae39ffc4a10b023fc5c7adfee803.jpg" class="arrows" target="_blank"><i class="fa fa-search-plus fa-fw"></i></a>
Regex picReg = new Regex(@"<a href=""(https://pic\.3gbizhi\.com/uploadmark/\d+/[a-zA-Z0-9]+\.(jpg|png))"" class=""arrows"" target=""_blank"">");
按钮的点击事件
string url = this.textBox1.Text;// 获取爬虫的url index_23.html
int start = int.Parse(this.textBox3.Text); //开始页数 index_1.html
int end = int.Parse(this.textBox4.Text); //结束页数 index_2.html
Regex reg = new Regex(@"index_\d+\.html$");
url = reg.Replace(url,""); //Replace =替换,把后面替换前面类型的字符串https://desk.3gbizhi.com/deskMV/
for (int i = start; i <=end; i++)
{string nowURL = $"{url}/index_{i}.html";HttpResponseMessage res = await Http.Client.GetAsync(nowURL);string data = await res.Content.ReadAsStringAsync();// 整体html字符串// 从data所有字符串匹配满足正则的字符串 返回结果是MatchCollection的数据集合MatchCollection maths = imgHtml.Matches(data);foreach (Match item in maths){ //下面需要根据html 匹配类型以下格式图片var res1 =  await Http.Client.GetAsync(picURL);string data1 = await res1.Content.ReadAsStringAsync();string picURL1 = picReg.Match(data1).Groups[1].Value;Console.WriteLine(picURL1);downLoad(picURL1);}
} 
 public async void downLoad(string url){var res =  await Http.Client.GetAsync(url);byte[] b1 = await res.Content.ReadAsByteArrayAsync();//C:\Users\Administrator\DesktopFile.WriteAllBytes(@"C:\Users\Administrator\Desktop\PP\"+Path.GetFileName(url), b1);}

http://www.lryc.cn/news/357476.html

相关文章:

  • 【本地运行chatgpt-web】启动前端项目和service服务端项目,也是使用nodejs进行开发的。两个都运行成功才可以使用!
  • TOGAF企业架构章节(核心)知识点(一)
  • 手摸手教你uniapp原生插件开发
  • C++进程间通信 消息队列
  • mysql中InnoDB的统计数据
  • P459 包装类Wrapper
  • Kong网关的负载均衡
  • 这是一个逗号
  • oracle tree
  • react-beautiful-dnd 横纵排序demo
  • web练习
  • 模型蒸馏笔记
  • HAL库使用FreeRTOS实时操作系统时配置时基源(TimeBase Source)
  • 如何让你的网站能通过域名访问
  • Spring Boot + Spring Security + JWT 从零开始
  • 【busybox记录】【shell指令】rmdir
  • [LitCTF 2023]yafu (中级) (素数分解)
  • MySQL alter 语句
  • 列表推导式(解析式)python
  • YOLO-10更快、更强
  • 新火种AI|寻求合作伙伴,展开豪赌,推出神秘AI项目...苹果能否突破AI困境?
  • MFC工控项目实例一主菜单制作
  • 代码随想录-Day22
  • uniapp项目 使用vue-plugin-hiprint静默打印功能
  • 视频汇聚EasyCVR视频监控平台GA/T 1400协议特点及应用领域解析
  • 基于似然场的快速避障算法
  • Flutter 中的 IndexedStack 小部件:全面指南
  • 基于51单片机的交通灯设计
  • ECMAScript 详解
  • 使用Java Swing制作一个飞翔的小鸟游戏