利用WebClient正则表达式下载资源
2017-10-18 17:00
633 查看
//我们拿下载图片举个例子
static WebClient client = new WebClient();
class Program
{
static WebClient client = new WebClient();
static WebClient client = new WebClient();
class Program
{
static WebClient client = new WebClient();
static void Main(string[] args) { string[] str = GetHvtImgUrls(@"http://www.imooc.com/");//下载地址 string url = ""; for (int i = 0; i < str.Length; i++) { url = str[i]; if (url.EndsWith(".jpg"))//判断是否是以jpg来结尾的 { if (!url.Contains("http:"))//判断是不是以hppt:开头的 { url = "http:" + url; } string filepath = @"D:\path练习\" + Guid.NewGuid() + ".jpg"; client.DownloadFile(url, filepath); } } } /// <summary> /// 取得HTML中所有图片的 URL。 /// </summary> /// <param name="sHtmlText">HTML代码</param> /// <returns>图片的URL列表</returns> public static string[] GetHvtImgUrls(string url) { string content = client.DownloadString(url);//获取返回的信息 // 定义正则表达式用来匹配 img 标签 (正则表达式很多,若想了解跟多欢迎点击https://baike.baidu.com/item/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/1700215?fr=aladdin) Regex m_hvtRegImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); // 搜索匹配的字符串 MatchCollection matches = m_hvtRegImg.Matches(content); int m_i = 0; string[] sUrlList = new string[matches.Count]; // 取得匹配项列表 foreach (Match match in matches) sUrlList[m_i++] = match.Groups["imgUrl"].Value; return sUrlList; }
相关文章推荐
- OC利用正则表达式获取网络资源(网络爬虫)
- OC利用正则表达式获取网络资源(网络爬虫)
- OC利用正则表达式获取网络资源(网络爬虫)
- OC利用正则表达式获取网络资源(网络爬虫)
- 利用requests和正则表达式re爬取猫眼电影top100,并下载图片
- 正则表达式应用--提取CSDN所有下载资源页面URl的思路与代码实现
- OC利用正则表达式获取网络资源(网络爬虫)
- OC利用正则表达式获取网络资源(网络爬虫)
- C#控制台基础 正则表达式,regex,webclient下载博客园网页中的一张图片
- VB.net文本框只允许输入数字与. 利用正则表达式验证IP地址
- SQL Server中利用正则表达式替换字符串
- js利用正则表达式去掉前后空格
- 利用正则表达式统计代码中的(代码行数,注释行数,空白行数)利用正则表达式获取一个网页中所有的邮箱地址
- 利用正则表达式判断四则运算表达式是否合法
- C#利用正则表达式实现字符串搜索
- 如何利用正则表达式ASP.NET字符是不是数字?
- C#中利用正则表达式去除HTML中的格式
- SQL Server中利用正则表达式替换字符串
- 利用正则表达式解析URL