单线程模式从网易下载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日--

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

posted @   逆火狂飙  阅读(220)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2019-03-05 [Canvas与艺术]Golden StopWatch 金色停表
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示