刺激战场
六合彩
贵宾厅
  • 1117阅读
  • 0回复

淘宝商品图片下载器 [复制链接]

上一主题 下一主题
离线寻幽老鬼
 

只看楼主 倒序阅读 使用道具 楼主  发表于: 2016-07-09


程序作用:下载淘宝商品图片:
    1、打开  运行.bat文件
    2、输入淘宝商品网址
    3、程序会自动在桌面建立文件夹,文件夹名称为商品名称
    4、程序会自动下载淘宝商品的展示图片到文件夹中
    5、输入  #  退出程序

下载:
  1. http://pan.baidu.com/s/1boJFyVl




注意:程序为java编写,,运行前安装请安装JRE,初版可能有时候会出现不稳定的情况,没关系,关闭后从新打开就行了,代码如下(昨天帮朋友赶出来的,有待优化):

  1. import java.io.ByteArrayOutputStream;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. import java.util.Random;
  10. import java.util.Scanner;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. import javax.swing.filechooser.FileSystemView;
  14. public class TB {
  15.     public static void main(String[] args) {
  16.          Scanner input = new Scanner(System.in);
  17.             String TBImgURL = null;       // 记录输入的字符串
  18.             do{
  19.                 System.out.println("请输入淘宝商品网址('输入#就推出'):");
  20.                 TBImgURL = input.next();       // 等待输入值
  21.                 if("#".equals(TBImgURL)){
  22.                     break;
  23.                 }
  24.                 if (!TBImgURL.matches("http.+")) {
  25.                     System.out.println("亲,输入的网址不对啊!");
  26.                 }else{
  27.                     getTBImg(TBImgURL);
  28.                 }
  29.             }while(!TBImgURL.equals("#"));   // 如果输入的值不是#就继续输入
  30.             System.out.println("程序已经退出!");
  31.             input.close(); // 关闭资源
  32.     }
  33.     public static void getTBImg(String TBImgURL) {
  34.         if (TBImgURL == null || TBImgURL.isEmpty() || "".equals(TBImgURL.trim())) {
  35.             System.out.println("没有获取url");
  36.             return;
  37.         }
  38.         int i_name = 0;
  39.         String imgUrl = "";
  40.         String imgPath = "";
  41.         String sp_name = "";
  42.         URL url=null;
  43.         URLConnection urlconn=null;
  44.         InputStream is = null;
  45.         File root=null;
  46.         File[] files=null;
  47.         try {
  48.             url = new URL(TBImgURL);
  49.             urlconn = url.openConnection();
  50.             urlconn.setDoOutput(true);
  51.             urlconn.setRequestProperty("User-Agent", "directclient");
  52.             urlconn.setRequestProperty("Accept-Charset", "utf-8");
  53.             urlconn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
  54.             urlconn.setRequestProperty("Connection", "Keep-Alive");
  55.             is = urlconn.getInputStream();
  56.             String newStr = inputStream2String(is, "gbk");
  57.             String regex_img = "//\\w+[.alicdn.com].+jpg";
  58.             String regex_taobao_title = "data-title=\".+\"";
  59.             String regex_tmall_title = "<title>.+</title>";
  60.             
  61.             Pattern p_img = Pattern.compile(regex_img);
  62.             Pattern p_taobao_title =  Pattern.compile(regex_taobao_title);
  63.             Pattern p_tmall_title =  Pattern.compile(regex_tmall_title);
  64.             
  65.             // 获取商品名称作为文件夹名称
  66.             Matcher m_taobao_title =p_taobao_title.matcher(newStr);
  67.             Matcher m_tmall_title =p_tmall_title.matcher(newStr);
  68.             
  69.             while (m_taobao_title.find()) {
  70.                 sp_name = new String(m_taobao_title.group().replace("data-title=", "").replace("\"", ""));
  71.             }
  72.             while (m_tmall_title.find()) {
  73.                 sp_name = new String(m_tmall_title.group().replace("<title>", "").replace("</title>", ""));
  74.             }
  75.             
  76.             
  77.             // 在桌面新建文件夹
  78.             File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
  79.             String desktopPath = desktopDir.getAbsolutePath();
  80.             if (desktopPath != null && !desktopPath.isEmpty() && !"".equals(desktopPath.trim())) {
  81.                 sp_name = sp_name.replaceAll(" */*", "");
  82.                 if (sp_name != null && !sp_name.isEmpty() && !"".equals(sp_name.trim())) {
  83.                     File folder = new File(desktopPath + "\\" + sp_name.replaceAll("-淘宝网*", "").replaceAll("-tmall.com天猫*", "").replaceAll("包邮", ""));
  84.                     folder.mkdir();
  85.                     imgPath = folder.getParentFile() + "\\" + folder.getName() + "\\";
  86.                 }
  87.             }
  88.             Matcher m_img = p_img.matcher(newStr);
  89.             while (m_img.find()) {
  90.                 imgUrl = "https:" + m_img.group();
  91.                 download(imgUrl, imgPath + i_name + new Random().nextInt(1000) + ".jpg");
  92.                 i_name++;
  93.             }
  94.         } catch (Exception e) {
  95.             System.out.println("程序可能出现了点小问题,如果没有影响使用可忽略");
  96.         } finally {
  97.             if(is!=null){
  98.                 try {
  99.                     is.close();
  100.                 } catch (Exception e2) {
  101.                 }
  102.             }
  103.         }
  104.         delFiles(imgPath);
  105.         root = new File(imgPath);
  106.         int fileSize=0;
  107.         if(root!=null){
  108.             files = root.listFiles();
  109.             if(files!=null && files.length>0){
  110.                 fileSize=files.length;
  111.             }
  112.         }
  113.         System.out.println("已经下载" + fileSize + "张图片保存到桌面了");
  114.     }
  115.     public static void download(String urlString, String filename) throws Exception {
  116.         // 构造URL
  117.         URL url = new URL(urlString);
  118.         // 打开连接
  119.         URLConnection con = url.openConnection();
  120.         // 输入流
  121.         InputStream is = con.getInputStream();
  122.         // 1K的数据缓冲
  123.         byte[] bs = new byte[1024];
  124.         // 读取到的数据长度
  125.         int len;
  126.         // 输出的文件流
  127.         OutputStream os = new FileOutputStream(filename);
  128.         // 开始读取
  129.         while ((len = is.read(bs)) != -1) {
  130.             os.write(bs, 0, len);
  131.         }
  132.         os.close();
  133.         is.close();
  134.     }
  135.     public static String inputStream2String(InputStream is, String charset) {
  136.         ByteArrayOutputStream baos = null;
  137.         try {
  138.             baos = new ByteArrayOutputStream();
  139.             int i = -1;
  140.             while ((i = is.read()) != -1) {
  141.                 baos.write(i);
  142.             }
  143.             return baos.toString(charset);
  144.         } catch (IOException e) {
  145.             e.printStackTrace();
  146.         } finally {
  147.             if (null != baos) {
  148.                 try {
  149.                     baos.close();
  150.                 } catch (IOException e) {
  151.                     e.printStackTrace();
  152.                 }
  153.                 baos = null;
  154.             }
  155.         }
  156.         return null;
  157.     }
  158.     
  159.     /**
  160.      * 过滤掉小图片
  161.      */
  162.     static void delFiles(String filePath) {
  163.         File root = new File(filePath);
  164.         if(root !=null){
  165.             File[] files = root.listFiles();
  166.             if(files!=null){
  167.                 if(files.length>0){
  168.                     for (File file : files) {
  169.                         if (file.isDirectory()) {
  170.                             if(file.length()<10000){
  171.                                 file.delete();
  172.                             }
  173.                         } else {
  174.                             if(file.length()<10000){
  175.                                 file.delete();
  176.                             }
  177.                         }
  178.                     }
  179.                 }
  180.             }
  181.         }
  182.     }
  183. }


快速回复
限100 字节
批量上传需要先选择文件,再选择上传
 
上一个 下一个