您的位置:首页 > 其它

批量下载凡客诚品的图片

2011-01-20 14:13 281 查看
凡客诚品的模特图片很多还是很pp的,有收藏癖想下载怎么办呢?自己动手。凡客的网页做的还是很简洁的,我没学过web相关的东西,所以只好用最原始的办法,正则表达式匹配html文件里的链接,幸好网站没用啥花哨技术。

程序现在只支持从大类页面开始解析,比如衬衫分类的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);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: