要下載圖片的頁面地址.jpg
正則表達(dá)式匹配下載地址.jpg
下載好的圖片存放在本地文件夾.jpg
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.IO;
namespace DownloadImageExample
{
class Program
{
static void Main(string[] args)
{
WebClient webClient = new WebClient();
//從要下載圖片的網(wǎng)頁地址下載所有網(wǎng)頁數(shù)據(jù)
string htmlStr = webClient.DownloadString(@"http://car.autohome.com.cn/photolist/series/3589/15/p1/?avareaid=101197");
//正則模式
string pattern = @"<img.+?(?<picSrc>http://car\d\..+?\.jpg).+?>";
//從網(wǎng)頁地址中匹配出所有合適的圖片地址
MatchCollection mc = Regex.Matches(htmlStr, pattern);
int i = 0;
foreach (Match item in mc)
{
if (item.Success)
{
i++;
string url = item.Groups["picSrc"].Value;//正則表達(dá)式匹配出的下載地址
string target = @"C:\Users\Sudo\桌面\Images\"+i+".jpg";//下載文件存放路徑
webClient.DownloadFile(url, target);//開始下載文件
}
}
Console.WriteLine("下載成功!S樽小沐飘!");
}
}
}