网络编程 —— 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);
 }
相关推荐
技术不好的崎鸣同学15 分钟前
Windows 命令提示符(CMD)恶意脚本分析篇
网络·windows·安全·系统安全
m0_7156744322 分钟前
2026年医疗行业数据库风险监测产品综合能力排名分析
网络·数据库
阿米亚波2 小时前
【EVE-NG 实战】防火墙基础(VLAN · VRRP · NAT · ACL · 静态路由)
运维·网络·笔记·网络协议·计算机网络·网络安全·运维开发
零意@2 小时前
海光平台debian11的系统,打开pstore功能和验证功能
网络
blueman88882 小时前
qt5-serialbus详细解析
开发语言·网络·qt
学究天人3 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷6)
网络·算法·数学建模·动态规划·几何学·图论·拓扑学
tiantianuser3 小时前
NVME-oF IP 设计2 :设计之前的调研!
网络·网络协议·rdma·roce v2·nvme of
星恒讯工业路由器4 小时前
工业无线通信选型常见问题解答
网络·信息与通信·无线ap·设备选型·工业无线通信·防爆无线设备·4g/5g路由器
用户125758524364 小时前
Go 里读取 request body 后,下游拿不到参数:别让日志中间件把请求吃掉
http·go·测试
智慧光迅AINOPOL4 小时前
校园全光网建设成本对比:全光网取代传统校园网建设成本分析
网络·全光网解决方案·全光网·校园全光网·校园全光网解决方案