• 单线程模式从网易下载A股叁仟捌佰支股票一年的交易数据耗时十四分钟


    代码下载:https://files.cnblogs.com/files/xiandedanteng/StockDataDownloader20200305.rar

    压缩包内包含股票代号文件,调整好日期目录,会运行Java程序就能下载。

    CoderFinder类代码:

    package com.hy;
    
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.regex.Pattern;
    
    public class CoderFinder {
        List<String> codeList;
    
        public CoderFinder(String filePathname) {
            codeList=new ArrayList<>();
            
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePathname), "UTF-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    String[] arr = line.split("\t+");
                        
                    if(isStockCode(arr[0])) {
                        codeList.add(arr[0]);
                    }
                }
                br.close();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        private static boolean isStockCode(String code) {
            final String patternStr = "\d+";
            return Pattern.matches(patternStr, code);
        }
        
        public List<String> getCodeList() {
            return codeList;
        }
        
        public static void main(String[] args) {
            long startMs=System.currentTimeMillis();
             
            CoderFinder cf=new CoderFinder("stockcodes.txt");
            List<String> ls=cf.getCodeList();
            Collections.sort(ls);
            
            StockFileDownloader downloader=new StockFileDownloader();
            
            int index=0;
            for(String code:cf.getCodeList()) {
                downloader.download(code, "20190101", "20200305", "C:\TEMP");
                System.out.println(String.format("#%d %s downloaded.", index,code));
                index++;
            }
            
            long endMs=System.currentTimeMillis();
            System.out.println("Time elapsed:"+ms2DHMS(startMs,endMs));
        }
        
        /**
         * change seconds to DayHourMinuteSecond format
         * 
         * @param startMs
         * @param endMs
         * @return
         */
        private static String ms2DHMS(long startMs, long endMs) {
            String retval = null;
            long secondCount = (endMs - startMs) / 1000;
            String ms = (endMs - startMs) % 1000 + "ms";
    
            long days = secondCount / (60 * 60 * 24);
            long hours = (secondCount % (60 * 60 * 24)) / (60 * 60);
            long minutes = (secondCount % (60 * 60)) / 60;
            long seconds = secondCount % 60;
    
            if (days > 0) {
                retval = days + "d" + hours + "h" + minutes + "m" + seconds + "s";
            } else if (hours > 0) {
                retval = hours + "h" + minutes + "m" + seconds + "s";
            } else if (minutes > 0) {
                retval = minutes + "m" + seconds + "s";
            } else if(seconds > 0) {
                retval = seconds + "s";
            }else {
                return ms;
            }
    
            return retval + ms;
        }
    }

    StockFileDownloader类代码:

    package com.hy;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.text.MessageFormat;
    
    public class StockFileDownloader {
        public void download(String originalCode,String fromDate,String toDate,String folder) {
            try {
               
                String code="";
                if(originalCode.startsWith("6")) {
                    code="0"+originalCode;
                }else {
                    code="1"+originalCode;
                }
                
                String raw="http://quotes.money.163.com/service/chddata.html?code={0}&start={1}&end={2}&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP";
                Object[] arr={code,fromDate,toDate};
                String urlPath = MessageFormat.format(raw, arr);
    
                URL url = new URL(urlPath);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.connect();
                int responseCode = connection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    InputStream inputStream = connection.getInputStream();
                    File dir = new File(folder);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    File file = new File(dir, originalCode+".txt");
                    FileOutputStream fos = new FileOutputStream(file);
                    byte[] buf = new byte[1024 * 8];
                    int len = -1;
                    while ((len = inputStream.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                    }
                    fos.flush();
                    fos.close();
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    --2020年3月5日--

    又完成一个小小的心愿,又前进了小小的一步。

  • 相关阅读:
    css
    css笔记
    css笔记
    echarts
    css笔记
    跨域
    JS案例
    html2canvas
    echarts
    echarts
  • 原文地址:https://www.cnblogs.com/heyang78/p/12422593.html
Copyright © 2020-2023  润新知