本文讲述了如何自动下载网页中的图片。首先获得网页的Html代码,然后用正则表达式分析其中图片的下载地址,最后自动逐个下载。
首先获得网页的Html代码,然后用正则表达式分析其中图片的下载地址,最后自动逐个下载。
Code
using System.Net;
/**///// <summary>
/// Download Image from website
/// better to put into threads
/// </summary>
private void DownloadImage()
{
WebClient c = new WebClient();
//Get html code
string content = c.DownloadString(Uri);
Collection<string> address = new Collection<string>();
Collection<string> name = new Collection<string>();
//Analyse html code to get images address(Uri) and (Name) list
ParseHtml(content, Prefix, PicUriPrefix + @"(?<Uri>[^""]*?)"">(?<Name>[^<]*?)</dt>", address, name);
if (address.Count > 0 && name.Count > 0 && address.Count == name.Count)
{
if (Directory.Exists(folder) == false) Directory.CreateDirectory(folder);//Create folder
foreach (string add in address)
{
//Download images one by one
c.DownloadFile(add, Path.Combine(folder, name[address.IndexOf(add)] + ".jpg"));
}
}
}
/**//// <summary>
/// Parse Html using regular expressions
/// </summary>
/// <param name="content">Html content</param>
/// <param name="prefix">Uri prefix</param>
/// <param name="expression">regular expression</param>
/// <param name="address">Image addresses collection</param>
/// <param name="name">Image names collection</param>
private void ParseHtml(string content, string prefix, string expression, Collection<string> address, Collection<string> name)
{
if (String.IsNullOrEmpty(expression) || address == null || name == null) return;
Regex re = new Regex(expression, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
MatchCollection mc = re.Matches(content);
if (mc == null || mc.Count == 0) return;
foreach (Match m in mc)
{
address.Add(prefix + m.Groups["Uri"].Value);
name.Add(m.Groups["Name"].Value);
}
}
Html部分代码如下:
Html code
alt="" src="/docfile/dyn/12345678LANGCCCCDDDDEEEEEEEERT09">BMW 3-series</dt>\par
alt="" src="/docfile/dyn/12345678LANGCCCCDDDDEEEEEEEEEE01">Toyota</dt>\par
alt="" src="/docfile/dyn/12345678LANGCCCCDDDDEEEEEEEEJY25">Polo cross</dt>\par
alt="" src="/docfile/dyn/12345678LANGCCCCDDDDEEEEEEEEMO02">Golf 4.5</dt>\par
要用正则表达式从中获得图片下载地址(例如:
/docfile/dyn/12345678LANGCCCCDDDDEEEEEEEERT09)和图片名称(例如:
BMW 3-series)
正则表达式部分如下:
Regular expression
@"/docfile/dyn/(?<Uri>[^""]*?)"">(?<Name>[^<]*?)</dt>"
注意其中group的用法(? *?),和引号的匹配用法,匹配双引号前面需要再带一个双引号。其中Uri这个group在dyn/后面,直到双引号结束;后面跟随着双引号和>,然后就是Name这个group;它以<号结束,最后跟随</dt>。