从通达信导出文件获取A股所有股票代号名称(至2020年2月24日)

下文是讲述如何从通达信的输出文件中获得股票信息,如果想用Java爬虫从网页爬取信息请参考:https://www.cnblogs.com/heyang78/p/12808381.html

要做个股票信息系统,首先要创建张基本表,表中包括股票代号和现用名。

要取得所有股票代号名称有很多方法,我分辨后觉得最方便快捷的是:

打开新浪通达信,敲入60,然后在菜单中选“系统”,“数据导出”,再选报表中所有数据,点导出,数据文件就会出现到C:\new_tdx\T0002\export\沪深A股20200224.txt 中了。

有图有真相:

当然,这个文件不能直接用,还需要用下面程序处理一下:

package readstocks;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;

public class StockReader {
    public void readFrom(String filePathname) {
        Map<String, String> map = new TreeMap<String, String>();

        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])) {
                    map.put(arr[0], arr[1]);
                }
            }
            br.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        PrintWriter out;
        try {
            out = new PrintWriter("C:\\Users\\ufo\\Desktop\\output.txt");

            int index = 0;
            for (String code : map.keySet()) {
                index++;
                String name = map.get(code);
                String raw = " insert into stock(id,code,name) values (''{0}'',''{1}'',''{2}'');";
                Object[] arr = { String.valueOf(index), code, name };
                String sql = MessageFormat.format(raw, arr);
                out.println(sql);
            }

            out.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    private boolean isStockCode(String str) {
        return Pattern.matches("\\d{6}", str);
    }

    public static void main(String[] args) {
        StockReader sr = new StockReader();
        sr.readFrom("C:\\new_tdx\\T0002\\export\\沪深A股20200224.txt");
    }
}

处理完了后output.txt就是我们需要的数据插入语句了,下面节选了一部分:

insert into stock(id,code,name) values ('1','000001','平安银行');
insert into stock(id,code,name) values ('2','000002','万 科A');
insert into stock(id,code,name) values ('3','000004','国农科技');
insert into stock(id,code,name) values ('4','000005','世纪星源');
insert into stock(id,code,name) values ('5','000006','深振业A');
insert into stock(id,code,name) values ('6','000007','全新好');
insert into stock(id,code,name) values ('7','000008','神州高铁');
insert into stock(id,code,name) values ('8','000009','中国宝安');
insert into stock(id,code,name) values ('9','000010','*ST美丽');
insert into stock(id,code,name) values ('10','000011','深物业A');

...

然后按下面语句建表:

create table stock(
   id number(6,0) primary key not null,
   code nvarchar2(6) not null,
   name nvarchar2(10) not null
)

插入完了以后,将上面大批insert一次性在sqlplus里执行完就行。下面是我执行的结果:

3791 688358 祥生医疗
3792 688363 华熙生物
3793 688366 昊海生科
3794 688368 晶丰明源
3795 688369 致远互联

ID CODE NAME
---------- ------------ --------------------
3796 688388 嘉元科技
3797 688389 普门科技
3798 688396 华润微
3799 688398 赛特新材
3800 688399 硕世生物

已选择3800行。

SQL> select count(*) from stock;

COUNT(*)
----------
3800

SQL> select * from stock where code='601857';

ID CODE NAME
---------- ------------ --------------------
3167 601857 中国石油

总共三千八百多支股票的stock表就建完了。

 

祝贺你看到这里,因为点击下面的链接你将直接得到三个文本的下载包:

https://files.cnblogs.com/files/xiandedanteng/stocks20200224.rar

 

--2020年2月24日--

posted @ 2020-02-24 09:27  逆火狂飙  阅读(6215)  评论(2编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东