废话少说,先上程序运行时的图片
开源地址:
https://github.com/xland/TuDao
编译好的版本下载地址:
https://github.com/xland/TuDao/blob/master/%E5%8F%91%E5%B8%83%E5%8C%85/%E5%B1%A0%E5%88%80.zip?raw=true
说明:
天猫店整店商品图片采集工具(包含题图、颜色图、内容图,适用于服装类,按货号保存商品图片),目前只测试了一个店铺,可能会存在一些问题,收费负责解决问题。
关键代码
下载图片的关键代码:
static void getPic(string url,string name) { ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.UseDefaultCredentials = true; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream stream = response.GetResponseStream(); var fileStream = new FileStream(name, FileMode.Create, FileAccess.Write); stream.CopyTo(fileStream); fileStream.Dispose(); stream.Close(); }
获取HTML的关键代码:
static string getHtml(string url) { ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.UseDefaultCredentials = true; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream, Encoding.Default); string html = reader.ReadToEnd(); stream.Close(); return html; }
请注意里面有一句UseDefaultCredentials,因为天猫整站都是https的,所以这一句还是挺有用的
下面是程序的业务逻辑代码:
Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("本程序分三步完成采集工作(每完成一部需要重启程序):"); Console.WriteLine("第1步:根据商户商品列表页面地址,采集商品编号"); Console.WriteLine("第2步:根据商品编号,采集商品图片地址"); Console.WriteLine("第3步:根据图片地址,下载图片"); Console.WriteLine("请问您现在需要执行第几步操作:(请输入1、2或3然后按任意键开始)"); var key = Console.ReadLine(); if(key == "1") { Console.WriteLine("请先输入目标商户的商品列表页面地址:"); baseListUrl = Console.ReadLine(); getId(); Console.WriteLine("第1步操作执行完毕,按任意键退出程序"); } else if(key == "2") { Console.WriteLine("开始执行第2步操作:"); prepareData(); Console.WriteLine("第2步操作执行完毕,按任意键退出程序"); } else if(key == "3") { Console.WriteLine("开始执行第3步操作:"); downloadPic(); Console.WriteLine("第3步操作执行完毕,按任意键退出程序"); } Console.ReadKey();
其他的代码请到GITHUB上去看吧,喜欢的请帮忙点个推荐,或者给我一颗GITHUB的星星!多谢!