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

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

搭建Form界面

Http类

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


}

图片所在页面网址的正则

cs 复制代码
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"">");
按钮的点击事件
cs 复制代码
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/
cs 复制代码
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);
    }
} 
cs 复制代码
 public async void downLoad(string url)
 {
    var res =  await Http.Client.GetAsync(url);
     byte[] b1 = await res.Content.ReadAsByteArrayAsync();
     //C:\Users\Administrator\Desktop
     File.WriteAllBytes(@"C:\Users\Administrator\Desktop\PP\"+Path.GetFileName(url), b1);
 }
相关推荐
QYRdata2 小时前
权威数据披露:2026至2032年边缘云服务CAGR达15.6%,赛道发展驶入快车道
网络·人工智能·云计算·服务发现·边缘计算
Yang96115 小时前
鼎讯信通DXB-2000S OTDR光时域反射仪模块技术拆解:0.8m盲区
网络
中科三方5 小时前
两家域名注册商资质被ICANN终止:企业域名资产安全再受关注
前端·网络·安全·域名
超智算科技5 小时前
2026服贸会现场直击|Net Zero Hub净零算力枢纽全球首发! 超智算受邀深度参与服贸会全球OPC共创节
网络·人工智能·科技·物联网·gpu算力
逐米时代5 小时前
工控网络安全:安全与连续性双目标下的精准防护
网络
Yang96115 小时前
通信广电与无线电管理:鼎讯信通ZN-080A手持式信号综合分析仪能做什么
网络
2601_949499946 小时前
DT‑1414PTZ如何百分百兼容(安华高)HFBR‑1414PTZ
运维·网络·人工智能·科技·光模块
sibylyue7 小时前
WebSocket双向长连接传输通道
网络·websocket·网络协议
小小、码农7 小时前
C++11右值引用与移动语义 —— 从拷贝资源到转移资源
开发语言·网络·c++·网络协议
Zenova EdgeOS8 小时前
工业网关数据持久化:从同步到 WAL 的工程实战
网络·数据库·oracle