WebClient wc = new WebClient();
//将网址位置赋值给字符串
string html = wc.DownloadString("https://news.cri.cn/gjjxs");
//寻找字符串里满足正则表达式的字符串,赋值给match集合
MatchCollection match = Regex.Matches(html, @"(/M00/[0-9A-Za-z]{2}/[0-9A-Za-z]{2}/([a-zA-Z0-9.]+.(jpg|png)))");
//输出字符串名称
//for (int i = 0; i < match.Count; i++)
//{
// Console.WriteLine(match[i].Value);
//}
//将match集合中的图片保存到相应的路径中
for (int i = 0; i < match.Count; i++)
{
if (match[i].Success)
{
//图片网页的前缀
string path = "https://p2.cri.cn";
//将图片前缀与图片名称合并就是图片在网页的位置,在将图片保存到自己电脑
wc.DownloadFile(path + match[i].Groups[1].Value, @"E:\国际漫画\" + match[i].Groups[2].Value);
Console.WriteLine("==");
}
}
Console.WriteLine("下载完成");
Console.ReadKey();
利用正则表达式下载网址内的图片
最新推荐文章于 2024-05-08 23:52:14 发布