批量下载凡客诚品的图片
2011-01-20 14:13
281 查看
凡客诚品的模特图片很多还是很pp的,有收藏癖想下载怎么办呢?自己动手。凡客的网页做的还是很简洁的,我没学过web相关的东西,所以只好用最原始的办法,正则表达式匹配html文件里的链接,幸好网站没用啥花哨技术。
程序现在只支持从大类页面开始解析,比如衬衫分类的http://www.vancl.com/CategoryList-1548-1--1/ChenShan.html,支持翻页,会把这个大类下面所有商品的所有大图下载下来,可惜的是不能分辨图片是衣服还是模特。要下载多个分类的话就再写个批处理,手工把每个大类的网址输进去。
下面是源代码,C#写的,下载部分直接用了WebClient类,所以效率很低,慢慢下吧。异常处理部分是后来硬加上去的,写的很烂,凑合用吧。
纯粹为了好玩写的,请勿滥用。如果你把图片挪作它用被凡客找上门的话,本人概不负责。
程序现在只支持从大类页面开始解析,比如衬衫分类的http://www.vancl.com/CategoryList-1548-1--1/ChenShan.html,支持翻页,会把这个大类下面所有商品的所有大图下载下来,可惜的是不能分辨图片是衣服还是模特。要下载多个分类的话就再写个批处理,手工把每个大类的网址输进去。
下面是源代码,C#写的,下载部分直接用了WebClient类,所以效率很低,慢慢下吧。异常处理部分是后来硬加上去的,写的很烂,凑合用吧。
纯粹为了好玩写的,请勿滥用。如果你把图片挪作它用被凡客找上门的话,本人概不负责。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Text.RegularExpressions; namespace VANCLpic { class Program { static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Usage: VANCLpic [URL]/nExample: VANCLpic http://www.vancl.com/CategoryList-1548-1--1/ChenShan.html"); return; } // 分类页面 String pageUrl = args[0]; Console.WriteLine("下载分类页面中..."); String page = null; try { WebClient wc = new WebClient(); Stream pageStream = wc.OpenRead(pageUrl); StreamReader sr = new StreamReader(pageStream); page = sr.ReadToEnd(); pageStream.Close(); sr.Close(); wc.Dispose(); } catch { Console.WriteLine("下载错误:" + pageUrl); } if (page != null) { String title = Regex.Match(page, "(?<=<title>)[^/a]*(?=</title>)").Value.Trim(); Console.WriteLine("/t" + title); // 改变工作目录 title = validFileName(title); Directory.CreateDirectory(title); System.Environment.CurrentDirectory = System.Environment.CurrentDirectory + "/" + title; // 处理首页 processProductList(pageUrl); } Console.WriteLine("全部下载完成"); } static String validFileName(String name) { if (name.Contains('//')) { name = name.Replace('//', ' '); } if (name.Contains('/')) { name = name.Replace('/', ' '); } if (name.Contains(':')) { name = name.Replace(':', ' '); } if (name.Contains('*')) { name = name.Replace('*', ' '); } if (name.Contains('?')) { name = name.Replace('?', ' '); } if (name.Contains('/"')) { name = name.Replace('/"', ' '); } if (name.Contains('<')) { name = name.Replace('<', ' '); } if (name.Contains('>')) { name = name.Replace('>', ' '); } if (name.Contains('|')) { name = name.Replace('|', ' '); } return name; } static void processProductList(string pageUrl) { Console.WriteLine("下载产品列表中..."); String page = null; try { WebClient wc = new WebClient(); Stream pageStream = wc.OpenRead(pageUrl); StreamReader sr = new StreamReader(pageStream); page = sr.ReadToEnd(); pageStream.Close(); sr.Close(); wc.Dispose(); } catch { Console.WriteLine("下载错误:" + pageUrl); } if (page != null) { Console.WriteLine("产品列表页面下载完成"); // 产品列表段 Match listProduct = Regex.Match(page, "<div id=/"listProduct/" >.*</div>"); // 单个产品的地址 MatchCollection matches = Regex.Matches(listProduct.Value, "(?<=<a href="/" mce_href="/"")//S*(?=/")"); // 去除重复 HashSet<String> productUrls = new HashSet<string>(); foreach (Match url in matches) { productUrls.Add(url.Value); } Console.WriteLine("共计" + productUrls.Count + "个产品"); // 处理每个产品 int counter = 0; foreach (String url in productUrls) { Console.WriteLine("处理产品" + (counter++) + "中..."); downloadProduct(url); } // 判断是否有下一页 Match nextPage = Regex.Match(page, "(?<=<dd class=/"pageDown/"><a href=/")[^/"]*(?=/")"); if (nextPage.Value != String.Empty) { Console.WriteLine("下一页"); String nextPageUrl = "http://www.vancl.com" + nextPage.Value; processProductList(nextPageUrl); } } } static void downloadProduct(String pageUrl) { try { WebClient wc = new WebClient(); Stream pageStream = wc.OpenRead(pageUrl); StreamReader sr = new StreamReader(pageStream); String page = sr.ReadToEnd(); Console.WriteLine("/t产品页面下载完成"); // 大图链接 Match entry = Regex.Match(page, "/Product/Pic.aspx.*(?=/")"); String picturePage = "http://www.vancl.com" + entry.Value; // 图片列表页面 pageStream = wc.OpenRead(picturePage); sr = new StreamReader(pageStream); page = sr.ReadToEnd(); MatchCollection matches = Regex.Matches(page, "(?<=<a href=")[^" mce_href=")[^"]*(?=')"); Console.WriteLine("/t共计" + matches.Count + "张图片"); // 下载每张图片 int counter = 0; foreach (Match url in matches) { Console.WriteLine("/t下载图片" + (counter++) + "中..."); String fileName = url.Value.Substring(url.Value.LastIndexOf('/') + 1); try { wc.DownloadFile(url.Value, fileName); } catch { Console.WriteLine("图片下载错误:" + url.Value); } } pageStream.Close(); sr.Close(); wc.Dispose(); } catch { Console.WriteLine("下载错误:" + pageUrl); } } } }
相关文章推荐
- Node.js monly图片批量下载爬虫1.00
- Node.js 4493图片批量下载爬虫1.00
- python3 批量下载网页所有图片
- 批量下载图片的代码实现
- 爱美之心.图片批量极速免费下载提取器 v2008 免费下载
- 爬虫实践---电影排行榜和图片批量下载
- Python实现的图片批量下载(v3.5最新)
- Node.js mzitu图片批量下载爬虫1.00
- 批量压缩图片软件下载
- python--批量下载豆瓣图片之升级版本
- 学以致用:批量下载豆瓣线上活动图片
- 一个用于批量下载网络图片的Shell脚本
- python下载批量图片
- php 图片批量下载
- 超囧的图片链接批量获取和下载
- 批量下载百度贴吧帖子图片
- 批量下载图片文件(服务器端 和 客户端)
- Node.js mimimn图片批量下载爬虫 1.00
- 基于Java实现批量下载网络图片
- 下载图片封装一个工具类,搞定图片批量下载