zoukankan      html  css  js  c++  java
  • 使用python获取股票数据

    1.接口

    http://qt.gtimg.cn/q=sz000001

    v_sz000001="51~平安银行~000001~9.52~9.48~9.50~671452~320552~350900~9.51~2107~9.50~4457~9.49~2416~9.48~1292~9.47~864~9.52~3203~9.53~6722~9.54~8049~9.55~24167~9.56~8702~15:00:03/9.52/7563/B/7199976/14926|14:57:00/9.50/108/S/102600/14837|14:56:57/9.51/39/B/37089/14835|14:56:54/9.51/55/B/52305/14831|14:56:51/9.51/2/B/1902/14827|14:56:48/9.51/2/B/1902/14824~20161208150133~0.04~0.42~9.55~9.43~9.50/663889/630858528~671452~63806~0.46~7.16~~9.55~9.43~1.27~1392.89~1634.62~0.92~10.43~8.53~1.01";

    http://qt.gtimg.cn/q=sh600004

    v_sh600004="1~白云机场~600004~14.64~14.60~14.62~55830~24874~30956~14.63~39~14.62~1722~14.61~95~14.60~496~14.59~86~14.64~57~14.65~539~14.66~201~14.67~660~14.68~604~14:59:52/14.64/3/B/4392/24876|14:59:46/14.64/10/B/14640/24870|14:59:17/14.64/5/B/7320/24820|14:58:37/14.64/10/B/14640/24756|14:58:16/14.63/6/S/8778/24721|14:57:56/14.64/2/B/2928/24683~20161208150857~0.04~0.27~14.70~14.57~14.64/55812/81726910~55830~8175~0.49~12.17~~14.70~14.57~0.89~168.36~168.36~1.58~16.06~13.14~0.99";

    2.准备

    MySQL数据库建表

    CREATE TABLE `stock` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `c0` varchar(8) DEFAULT NULL COMMENT '未知',
      `c1` varchar(24) DEFAULT NULL COMMENT '名字',
      `c2` varchar(8) DEFAULT NULL COMMENT '代码',
      `c3` double DEFAULT NULL COMMENT '当前价格',
      `c4` double DEFAULT NULL COMMENT '昨收',
      `c5` double DEFAULT NULL COMMENT '今开',
      `c6` int(11) DEFAULT NULL COMMENT '成交量(手)',
      `c7` int(11) DEFAULT NULL COMMENT '外盘',
      `c8` int(11) DEFAULT NULL COMMENT '内盘',
      `c9` double DEFAULT NULL COMMENT '买一',
      `c10` int(11) DEFAULT NULL COMMENT '买一量(手)',
      `c11` double DEFAULT NULL COMMENT '买二',
      `c12` int(11) DEFAULT NULL COMMENT '买二量(手)',
      `c13` double DEFAULT NULL COMMENT '买三',
      `c14` int(11) DEFAULT NULL COMMENT '买三量(手)',
      `c15` double DEFAULT NULL COMMENT '买四',
      `c16` int(11) DEFAULT NULL COMMENT '买四量(手)',
      `c17` double DEFAULT NULL COMMENT '买五',
      `c18` int(11) DEFAULT NULL COMMENT '买五量(手)',
      `c19` double DEFAULT NULL COMMENT '卖一',
      `c20` int(11) DEFAULT NULL COMMENT '卖一量',
      `c21` double DEFAULT NULL COMMENT '卖二',
      `c22` int(11) DEFAULT NULL COMMENT '卖二量(手)',
      `c23` double DEFAULT NULL COMMENT '卖三',
      `c24` int(11) DEFAULT NULL COMMENT '卖三量(手)',
      `c25` double DEFAULT NULL COMMENT '卖四',
      `c26` int(11) DEFAULT NULL COMMENT '卖四量(手)',
      `c27` double DEFAULT NULL COMMENT '卖五',
      `c28` int(11) DEFAULT NULL COMMENT '卖五量(手)',
      `c29` varchar(256) DEFAULT NULL COMMENT '最近逐笔成交',
      `c30` varchar(16) DEFAULT NULL COMMENT '时间',
      `c31` double DEFAULT NULL COMMENT '涨跌',
      `c32` double DEFAULT NULL COMMENT '涨跌%',
      `c33` double DEFAULT NULL COMMENT '最高',
      `c34` double DEFAULT NULL COMMENT '最低',
      `c35` varchar(32) DEFAULT NULL COMMENT '价格/成交量(手)/成交额',
      `c36` int(11) DEFAULT NULL COMMENT '成交量(手)',
      `c37` int(11) DEFAULT NULL COMMENT '成交额(万)',
      `c38` int(11) DEFAULT NULL COMMENT '换手率',
      `c39` double DEFAULT NULL,
      `c40` varchar(16) DEFAULT NULL,
      `c41` double DEFAULT NULL COMMENT '最高',
      `c42` double DEFAULT NULL COMMENT '最低',
      `c43` double DEFAULT NULL COMMENT '振幅',
      `c44` double DEFAULT NULL COMMENT '流通市值',
      `c45` double DEFAULT NULL COMMENT '总市值',
      `c46` double DEFAULT NULL COMMENT '市净率',
      `c47` double DEFAULT NULL COMMENT '涨停价',
      `c48` double DEFAULT NULL COMMENT '跌停价',
      `c49` double DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=507 DEFAULT CHARSET=utf8mb4;

    3.python代码

    #
    import MySQLdb
    import re
    import urllib2
    
    conn = MySQLdb.connect(
    	host='local',
    	port=3306,
    	user='root',
    	passwd='123456',
    	db='test',
    	charset='utf8',
    	)
    
    cur = conn.cursor()
    
    for i in range(1,3000):
    	url = 'http://qt.gtimg.cn/q=sz%06d' % i
    	print url,
    	req = urllib2.Request(url)
    	response = urllib2.urlopen(req)
    	content = response.read().decode('GBK')
    	row = re.findall('^v_(sh|sz)d{6}="(.*)";', content)
    	if len(row) == 1:
    		cols = row[0][1].split('~')
    		if cols[39]=='':
    			cols[39]=None
    			print 'c39=null',
    		try:
    			cur.execute("INSERT INTO stock (c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,
    			c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,
    			c20,c21,c22,c23,c24,c25,c26,c27,c28,c29,
    			c30,c31,c32,c33,c34,c35,c36,c37,c38,c39,
    			c40,c41,c42,c43,c44,c45,c46,c47,c48,c49)
    			VALUES (
    			%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,
    			%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,
    			%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,
    			%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,
    			%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", cols)
    			print '...[OK]' 
    		except MySQLdb.Error,e:
    			print "%d %s", e.args[0], e.args[1]
    			print sql
    	else:
    		print content,
    		print '...[Failed]'
    	if i%50 == 0:
    		conn.commit()
    		print 'Commit per 100 result.'
    cur.close()
    conn.close()
  • 相关阅读:
    Nodejs下载和第一个Nodejs示例
    永久关闭Win10工具栏的TaskbarSearch控件
    对称加密,非对称加密,散列算法,签名算法
    【转】TTL和RS232之间的详细对比
    zlg核心板linux系统中查看系统内存等使用信息
    Power BI后台自动刷新数据报错 The operation was throttled by Power BI Premium because there were too many datasets being processed concurrently.
    剪切板和上传文件内容获取
    CSS, LESS, SCSS, SASS总结
    文字程序
    electron 打包“ERR_ELECTRON_BUILDER_CANNOT_EXECUTE”
  • 原文地址:https://www.cnblogs.com/ilovecpp/p/12749937.html
Copyright © 2011-2022 走看看