比如输入:
http://quotes.money.163.com/service/chddata.html?code=0601857&start=20200101&end=20200224&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP
则会得到一个下载下来的CSV文件,其内容节选如下:
日期,股票代码,名称,收盘价,最高价,最低价,开盘价,前收盘,涨跌额,涨跌幅,换手率,成交量,成交金额,总市值,流通市值
2020-02-21,'601857,中国石油,5.37,5.38,5.34,5.35,5.38,-0.01,-0.1859,0.0703,113816298,609926963.0,9.82822650883e+11,8.69521557883e+11
2020-02-20,'601857,中国石油,5.38,5.38,5.32,5.35,5.33,0.05,0.9381,0.0652,105637160,564952420.0,9.84652860661e+11,8.71140778661e+11
2020-02-19,'601857,中国石油,5.33,5.36,5.32,5.35,5.35,-0.02,-0.3738,0.0458,74189083,395688944.0,9.7550181177e+11,8.6304467477e+11
2020-02-18,'601857,中国石油,5.35,5.38,5.33,5.38,5.38,-0.03,-0.5576,0.0458,74201915,396980717.0,9.79162231326e+11,8.66283116326e+11
2020-02-17,'601857,中国石油,5.38,5.38,5.27,5.29,5.28,0.1,1.8939,0.0521,84392095,450155266.0,9.84652860661e+11,8.71140778661e+11
2020-02-14,'601857,中国石油,5.28,5.3,5.25,5.26,5.28,0.0,0.0,0.0296,47917020,252872541.0,9.66350762879e+11,8.54948570879e+11
2020-02-13,'601857,中国石油,5.28,5.35,5.28,5.33,5.31,-0.03,-0.565,0.0331,53568587,284224188.0,9.66350762879e+11,8.54948570879e+11
。。。
这个挺贴心。
参数说明:代码为股票代码,上海股票前加0,如600756变成0600756,深圳股票前加1。时间都是6位标识法,年月日,fields标识想要请求的数据。可以不变。
例如大盘指数数据查询(上证指数000001前加0,沪深300指数000300股票前加0,深证成指399001前加1,中小板指399005前加1,创业板指399006前加1):
http://quotes.money.163.com/service/chddata.html?code=0000300&start=20151219&end=20171108&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;VOTURNOVER
当然,手工一个个去点三千八百多个股票会烦死,用程序进行处理就舒服多了,下面代码实现了沪深两支股票的下载:
package urlconn; 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 StockFileDownloader(String originalCode,String fromDate,String toDate,String folder) { try { // Proxyconfiguration System.setProperty("http.proxyHost", "pkg.proxy.prod.jp.local"); System.setProperty("http.proxyPort", "10080"); 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(); } } public static void main(String[] args) { new StockFileDownloader("601857","20191201","20200201","D:\logs\"); new StockFileDownloader("000762","20191201","20200201","D:\logs\"); } }
下载数据一:
日期,股票代码,名称,收盘价,最高价,最低价,开盘价,前收盘,涨跌额,涨跌幅,换手率,成交量,成交金额,总市值,流通市值
2020-01-23,'000762,西藏矿业,7.75,8.23,7.69,8.1,8.17,-0.42,-5.1408,1.9612,10214269,81187816.25,4036349110.0,4036349110.0
2020-01-22,'000762,西藏矿业,8.17,8.18,7.87,7.87,7.96,0.21,2.6382,1.8112,9433278,76121806.75,4255093190.8,4255093190.8
2020-01-21,'000762,西藏矿业,7.96,8.0,7.85,7.99,7.98,-0.02,-0.2506,1.5107,7868043,62306620.06,4145721150.4,4145721150.4
2020-01-20,'000762,西藏矿业,7.98,8.06,7.8,8.01,8.0,-0.02,-0.25,1.831,9536417,75550797.07,4156137535.2,4156137535.2
2020-01-17,'000762,西藏矿业,8.0,8.15,7.98,8.06,8.04,-0.04,-0.4975,1.7123,8917984,71788585.66,4166553920.0,4166553920.0
2020-01-16,'000762,西藏矿业,8.04,8.18,7.93,8.12,8.16,-0.12,-1.4706,2.3931,12463776,100137407.16,4187386689.6,4187386689.6
2020-01-15,'000762,西藏矿业,8.16,8.28,8.06,8.28,8.33,-0.17,-2.0408,3.063,15952551,129865547.67,4249884998.4,4249884998.4
2020-01-14,'000762,西藏矿业,8.33,8.56,8.11,8.26,8.16,0.17,2.0833,4.7634,24808728,207791032.91,4338424269.2,4338424269.2
2020-01-13,'000762,西藏矿业,8.16,8.38,7.9,7.9,7.88,0.28,3.5533,3.0358,15811132,128604848.83,4249884998.4,4249884998.4
2020-01-10,'000762,西藏矿业,7.88,8.03,7.83,8.02,8.07,-0.19,-2.3544,2.1784,11345474,89772947.54,4104055611.2,4104055611.2
2020-01-09,'000762,西藏矿业,8.07,8.11,7.64,7.69,7.68,0.39,5.0781,3.9162,20396342,161081261.1,4203011266.8,4203011266.8
2020-01-08,'000762,西藏矿业,7.68,7.95,7.58,7.6,7.64,0.04,0.5236,3.1569,16441537,127597627.93,3999891763.2,3999891763.2
2020-01-07,'000762,西藏矿业,7.64,7.73,7.6,7.64,7.65,-0.01,-0.1307,1.8234,9496777,72667291.9,3979058993.6,3979058993.6
2020-01-06,'000762,西藏矿业,7.65,7.73,7.33,7.4,7.42,0.23,3.0997,3.024,15749336,119350099.16,3984267186.0,3984267186.0
2020-01-03,'000762,西藏矿业,7.42,7.56,7.41,7.54,7.49,-0.07,-0.9346,2.0709,10785648,80437819.12,3864478760.8,3864478760.8
2020-01-02,'000762,西藏矿业,7.49,7.56,7.42,7.56,7.46,0.03,0.4021,1.9426,10117451,75661387.55,3900936107.6,3900936107.6
2019-12-31,'000762,西藏矿业,7.46,7.6,7.36,7.46,7.47,-0.01,-0.1339,1.8302,9532270,71259135.64,3885311530.4,3885311530.4
2019-12-30,'000762,西藏矿业,7.47,7.69,7.3,7.34,7.43,0.04,0.5384,2.6632,13870585,103652901.0,3890519722.8,3890519722.8
2019-12-27,'000762,西藏矿业,7.43,7.81,7.14,7.29,7.22,0.21,2.9086,3.9287,20461258,151378980.82,3869686953.2,3869686953.2
2019-12-26,'000762,西藏矿业,7.22,7.33,7.09,7.12,7.11,0.11,1.5471,2.0152,10495411,75827487.49,3760314912.8,3760314912.8
2019-12-25,'000762,西藏矿业,7.11,7.14,7.03,7.03,7.1,0.01,0.1408,0.9034,4705276,33338186.79,3703024796.4,3703024796.4
2019-12-24,'000762,西藏矿业,7.1,7.15,6.82,6.85,6.84,0.26,3.8012,1.4212,7402100,52092916.92,3697816604.0,3697816604.0
2019-12-23,'000762,西藏矿业,6.84,7.01,6.82,7.01,7.01,-0.17,-2.4251,0.8099,4217901,29090681.24,3562403601.6,3562403601.6
2019-12-20,'000762,西藏矿业,7.01,7.09,6.99,7.05,7.05,-0.04,-0.5674,0.7102,3699028,26042076.1,3650942872.4,3650942872.4
2019-12-19,'000762,西藏矿业,7.05,7.07,7.01,7.07,7.02,0.03,0.4274,0.7177,3737721,26330070.05,3671775642.0,3671775642.0
2019-12-18,'000762,西藏矿业,7.02,7.09,7.0,7.08,7.06,-0.04,-0.5666,0.6985,3637733,25587868.54,3656151064.8,3656151064.8
2019-12-17,'000762,西藏矿业,7.06,7.09,6.9,6.95,6.95,0.11,1.5827,1.3486,7023734,49371184.13,3676983834.4,3676983834.4
2019-12-16,'000762,西藏矿业,6.95,6.95,6.81,6.85,6.83,0.12,1.757,0.7732,4027151,27792449.9,3619693718.0,3619693718.0
2019-12-13,'000762,西藏矿业,6.83,6.85,6.75,6.75,6.75,0.08,1.1852,0.4512,2350167,16001968.93,3557195409.2,3557195409.2
2019-12-12,'000762,西藏矿业,6.75,6.83,6.73,6.79,6.81,-0.06,-0.8811,0.361,1880075,12723705.5,3515529870.0,3515529870.0
2019-12-11,'000762,西藏矿业,6.81,6.87,6.79,6.87,6.89,-0.08,-1.1611,0.421,2192430,14955761.95,3546779024.4,3546779024.4
2019-12-10,'000762,西藏矿业,6.89,6.92,6.79,6.84,6.85,0.04,0.5839,0.5261,2740002,18822008.57,3588444563.6,3588444563.6
2019-12-09,'000762,西藏矿业,6.85,6.95,6.8,6.91,6.89,-0.04,-0.5806,0.6481,3375354,23184943.78,3567611794.0,3567611794.0
2019-12-06,'000762,西藏矿业,6.89,6.96,6.89,6.95,6.94,-0.05,-0.7205,0.4201,2187900,15128251.0,3588444563.6,3588444563.6
2019-12-05,'000762,西藏矿业,6.94,6.96,6.89,6.9,6.89,0.05,0.7257,0.336,1750207,12127521.38,3614485525.6,3614485525.6
2019-12-04,'000762,西藏矿业,6.89,6.96,6.88,6.96,6.98,-0.09,-1.2894,0.4837,2519384,17443055.39,3588444563.6,3588444563.6
2019-12-03,'000762,西藏矿业,6.98,7.05,6.94,7.05,7.06,-0.08,-1.1331,0.4506,2346955,16349607.25,3635318295.2,3635318295.2
2019-12-02,'000762,西藏矿业,7.06,7.09,6.92,7.0,7.07,-0.01,-0.1414,0.8957,4664913,32634872.15,3676983834.4,3676983834.4
下载数据二:
日期,股票代码,名称,收盘价,最高价,最低价,开盘价,前收盘,涨跌额,涨跌幅,换手率,成交量,成交金额,总市值,流通市值
2020-01-23,'601857,中国石油,5.64,5.72,5.62,5.7,5.74,-0.1,-1.7422,0.0503,81444447,460859100.0,1.03223831489e+12,9.13240518894e+11
2020-01-22,'601857,中国石油,5.74,5.77,5.71,5.76,5.79,-0.05,-0.8636,0.0409,66239738,379676573.0,1.05054041268e+12,9.29432726675e+11
2020-01-21,'601857,中国石油,5.79,5.85,5.78,5.85,5.85,-0.06,-1.0256,0.0346,56028546,324824586.0,1.05969146157e+12,9.37528830566e+11
2020-01-20,'601857,中国石油,5.85,5.86,5.81,5.85,5.84,0.01,0.1712,0.033,53408389,311510573.0,1.07067272024e+12,9.47244155235e+11
2020-01-17,'601857,中国石油,5.84,5.87,5.83,5.84,5.83,0.01,0.1715,0.0226,36667641,214376983.0,1.06884251046e+12,9.45624934457e+11
2020-01-16,'601857,中国石油,5.83,5.89,5.81,5.88,5.87,-0.04,-0.6814,0.0357,57860992,337630232.0,1.06701230068e+12,9.44005713679e+11
2020-01-15,'601857,中国石油,5.87,5.97,5.86,5.95,5.95,-0.08,-1.3445,0.0445,72014546,424495017.0,1.07433313979e+12,9.50482596792e+11
2020-01-14,'601857,中国石油,5.95,5.98,5.94,5.95,5.97,-0.02,-0.335,0.0275,44480419,265271834.0,1.08897481802e+12,9.63436363017e+11
2020-01-13,'601857,中国石油,5.97,5.99,5.93,5.99,5.99,-0.02,-0.3339,0.034,55118392,327829650.0,1.09263523757e+12,9.66674804573e+11
2020-01-10,'601857,中国石油,5.99,6.04,5.96,6.04,6.06,-0.07,-1.1551,0.0569,92130621,551691385.0,1.09629565713e+12,9.6991324613e+11
2020-01-09,'601857,中国石油,6.06,6.08,6.0,6.04,6.23,-0.17,-2.7287,0.0988,159983683,966146262.0,1.10910712558e+12,9.81247791577e+11
2020-01-08,'601857,中国石油,6.23,6.3,6.15,6.26,6.11,0.12,1.964,0.1331,215451333,1337592338.0,1.14022069181e+12,1.00877454481e+12
2020-01-07,'601857,中国石油,6.11,6.17,6.07,6.17,6.23,-0.12,-1.9262,0.0943,152681755,931508895.0,1.11825817447e+12,9.89343895468e+11
2020-01-06,'601857,中国石油,6.23,6.28,6.02,6.03,5.95,0.28,4.7059,0.1863,301646124,1859195864.0,1.14022069181e+12,1.00877454481e+12
2020-01-03,'601857,中国石油,5.95,5.99,5.86,5.87,5.87,0.08,1.3629,0.074,119856782,712671271.0,1.08897481802e+12,9.63436363017e+11
2020-01-02,'601857,中国石油,5.87,5.88,5.84,5.86,5.83,0.04,0.6861,0.045,72808500,427014028.0,1.07433313979e+12,9.50482596792e+11
2019-12-31,'601857,中国石油,5.83,5.84,5.78,5.8,5.82,0.01,0.1718,0.0245,39722302,230670803.0,1.06701230068e+12,9.44005713679e+11
2019-12-30,'601857,中国石油,5.82,5.83,5.75,5.78,5.78,0.04,0.692,0.0315,51029015,296089973.0,1.0651820909e+12,9.42386492901e+11
2019-12-27,'601857,中国石油,5.78,5.83,5.73,5.73,5.73,0.05,0.8726,0.0405,65523742,378993114.0,1.05786125179e+12,9.35909609788e+11
2019-12-26,'601857,中国石油,5.73,5.73,5.7,5.71,5.72,0.01,0.1748,0.0189,30654684,175257139.0,1.0487102029e+12,9.27813505897e+11
2019-12-25,'601857,中国石油,5.72,5.74,5.7,5.74,5.73,-0.01,-0.1745,0.0149,24126858,137916555.0,1.04687999312e+12,9.26194285119e+11
2019-12-24,'601857,中国石油,5.73,5.74,5.7,5.71,5.71,0.02,0.3503,0.0184,29824820,170607073.0,1.0487102029e+12,9.27813505897e+11
2019-12-23,'601857,中国石油,5.71,5.81,5.7,5.8,5.8,-0.09,-1.5517,0.0298,48299424,277548625.0,1.04504978334e+12,9.24575064341e+11
2019-12-20,'601857,中国石油,5.8,5.85,5.8,5.83,5.82,-0.02,-0.3436,0.0248,40194898,233871593.0,1.06152167134e+12,9.39148051344e+11
2019-12-19,'601857,中国石油,5.82,5.84,5.8,5.81,5.82,0.0,0.0,0.0207,33502811,194816521.0,1.0651820909e+12,9.42386492901e+11
2019-12-18,'601857,中国石油,5.82,5.86,5.81,5.85,5.85,-0.03,-0.5128,0.0329,53227076,310647399.0,1.0651820909e+12,9.42386492901e+11
2019-12-17,'601857,中国石油,5.85,5.87,5.74,5.77,5.77,0.08,1.3865,0.0657,106313905,618670130.0,1.07067272024e+12,9.47244155235e+11
2019-12-16,'601857,中国石油,5.77,5.78,5.68,5.71,5.69,0.08,1.406,0.045,72787069,417173933.0,1.05603104201e+12,9.3429038901e+11
2019-12-13,'601857,中国石油,5.69,5.72,5.64,5.66,5.63,0.06,1.0657,0.0523,84648652,481767289.0,1.04138936378e+12,9.21336622784e+11
2019-12-12,'601857,中国石油,5.63,5.66,5.63,5.65,5.65,-0.02,-0.354,0.0238,38523075,217461050.0,1.03040810512e+12,9.11621298115e+11
2019-12-11,'601857,中国石油,5.65,5.66,5.63,5.66,5.65,0.0,0.0,0.0233,37683524,212757819.0,1.03406852467e+12,9.14859739672e+11
2019-12-10,'601857,中国石油,5.65,5.66,5.6,5.64,5.65,0.0,0.0,0.0298,48289410,271893693.0,1.03406852467e+12,9.14859739672e+11
2019-12-09,'601857,中国石油,5.65,5.66,5.61,5.64,5.64,0.01,0.1773,0.0265,42943647,241968933.0,1.03406852467e+12,9.14859739672e+11
2019-12-06,'601857,中国石油,5.64,5.64,5.61,5.63,5.63,0.01,0.1776,0.0186,30154291,169639428.0,1.03223831489e+12,9.13240518894e+11
2019-12-05,'601857,中国石油,5.63,5.63,5.59,5.61,5.58,0.05,0.8961,0.025,40544079,227687970.0,1.03040810512e+12,9.11621298115e+11
2019-12-04,'601857,中国石油,5.58,5.58,5.55,5.56,5.58,0.0,0.0,0.0166,26814545,149209268.0,1.02125705622e+12,9.03525194224e+11
2019-12-03,'601857,中国石油,5.58,5.58,5.54,5.57,5.56,0.02,0.3597,0.018,29182871,162205441.0,1.02125705622e+12,9.03525194224e+11
2019-12-02,'601857,中国石油,5.56,5.59,5.55,5.57,5.59,-0.03,-0.5367,0.0193,31282800,174129366.0,1.01759663667e+12,9.00286752668e+11
还可以用线程调用,循环调用方式,这里就不再赘述了,请大家自行实现。
参考网页:
https://www.cnblogs.com/baofengyu/p/6393310.html
HttpUrlConnection使用详解: https://www.cnblogs.com/tenWood/p/8563617.html
HttpUrlConnection使用代理: https://www.cnblogs.com/exmyth/p/6493285.html