使用Python获取股票行情数据(中国个股/中国指数/全球指数)

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# author: https://blog.csdn.net/bitquant
#中国股票数据

import urllib.request
import time, datetime
import re, json


class STOCK():

    _prefix_url = "https://hq.sinajs.cn/list="

    #中国指数
    def index(self, code):
        exchange = "sz" if (code[:3] == "399") else "sh"
        url = self._prefix_url + exchange + code       #http://hq.sinajs.cn/list=s_sh000001
        info = self.http_request(url)
        result = self.data_format(info)
        return result

    #美股,港股指数
    def index_world(self, ex_code):
        url = self._prefix_url + ex_code       #http://hq.sinajs.cn/list=s_sh000001
        info = self.http_request(url)
        result = self.data_format(info)
        return result

    #中国个股
    def individual(self, code):
        if ((code[:3] == "600") | (code[:3] == "900") | (code[:3] == "688")):   #上证A股600,上证B股900,科创板688
            exchange = "sh"
        elif ((code[:2] == "43") | (code[:2] == "83") | (code[:2] == "87")): #新三板43和83,87
            exchange = "sb"
        elif ((code[:3] == "000") | (code[:3] == "200")): #深证A股000,深证B股200
            exchange = "sz"
        elif ((code[:3] == "002")): #中小板002
            exchange = "sz"
        elif ((code[:3] == "300")): #创业板300
            exchange = "sz"
        # else:
            # exchange = "sz"        #其他默认sz
        url = self._prefix_url + exchange + code
        info = self.http_request(url)
        result = self.data_format(info)
        return result

    #数据标准化函数
    def data_format(self, data):
        rawdata = re.search('(")(.+)(")', data).group(2)
        stockdata = rawdata.split(",")
        if ((stockdata[0] == "道琼斯") | (stockdata[0] == "纳斯达克") | (stockdata[0] == "标普500指数")):
            ts = int(time.mktime(time.strptime(stockdata[3], "%Y-%m-%d %H:%M:%S")))
            dict = {'code': code,
                    'name': stockdata[0],
                    'open': float(stockdata[5]),
                    'high': float(stockdata[6]),
                    'low': float(stockdata[7]),
                    'close': float(stockdata[1]),
                    'volume': float(stockdata[10]), #成交量
                    'amount': None,
                    'time': ts
                    }

        elif (stockdata[1] == "恒生指数"):
            ts = int(time.mktime(time.strptime('%s %s'%(stockdata[17],stockdata[18]) , "%Y/%m/%d %H:%M:%S")))
            dict = {'code': code,
                    'name': stockdata[1],
                    'open': float(stockdata[2]),
                    'high': float(stockdata[4]),
                    'low': float(stockdata[5]),
                    'close': float(stockdata[6]),
                    'volume': None,       #成交量
                    'amount': float(stockdata[11]),       #成交额
                    'time': ts
                    }
        else:
            ts = int(time.mktime(time.strptime('%s %s'%(stockdata[30],stockdata[31]) , "%Y-%m-%d %H:%M:%S")))
            dict = {'code': code,
                    'name': stockdata[0],
                    'open': float(stockdata[1]),
                    'high': float(stockdata[4]),
                    'low': float(stockdata[5]),
                    'close': float(stockdata[3]),
                    'volume': float(stockdata[8]),       #成交量
                    'amount': float(stockdata[9]),       #成交额
                    'time': ts
                    }
        return dict

    # HTTP请求
    def http_request(self, url):
        try:
            req = urllib.request.Request(url)
            req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36')
            r = urllib.request.urlopen(req)
            result = r.read().decode('gb2312')
            return result
        except Exception as e:
            print([url, repr(e)])

if __name__ == "__main__":
    stock = STOCK()

    #遍历国内主要指数
    chinaindexes = [
        "000001", # sh000001 上证指数
        "399001", # sz399001 深证成指
        "000300", # sh000300 沪深300
        "399005", # sz399005 中小板指
        "399006", # sz399006 创业板指
        "000003", # sh000003 B股指数
    ]
    for code in chinaindexes:
        result = stock.index(code)
        print(result)


    #遍历国内股票
    chinastockes = [
        "000063",   #中兴通讯
        "600036",   #招商银行
        "600519",   #贵州茅台
        "300431",   #暴风科技
        "600050",   #上A
        "900925",   #上B
        "000063",   #深A
        "200026",   #深B
        "002097",   #中小板
        "300003",   #创业板
        "871003",   #新三板
        "688029",   #科创板
    ]
    for code in chinastockes:
        result = stock.individual(code)
        print(result)

    #美股,港股指数
    # https://hq.sinajs.cn/list=int_dji,int_nasdaq,int_sp500,int_hangseng 简版
    # https://hq.sinajs.cn/list=gb_dji,gb_ixic,gb_inx,rt_hkHSI              复杂版
    worldstockes = [
        "gb_dji",   #  道琼斯
        "gb_ixic",  #  纳斯达克
        "gb_inx",   #  标普指数
        "rt_hkHSI", #  恒生指数
    ]
    for code in worldstockes:
        result = stock.index_world(code)
        print(result)


返回如下

{'code': '000001', 'name': '上证指数', 'open': 2812.2422, 'high': 2832.6662, 'low': 2802.9579, 'close': 2815.4947, 'volume': 210130981.0, 'amount': 22
5293706289.0, 'time': 1587970920}
{'code': '399001', 'name': '深证成指', 'open': 10449.127, 'high': 10531.392, 'low': 10367.292, 'close': 10452.171, 'volume': 32036978713.0, 'amount':
331351587770.756, 'time': 1587970803}
{'code': '000300', 'name': '沪深300', 'open': 3808.0171, 'high': 3842.8706, 'low': 3793.7655, 'close': 3822.769, 'volume': 95462735.0, 'amount': 14530
1151875.0, 'time': 1587970920}
{'code': '399005', 'name': '中小板指', 'open': 6827.842, 'high': 6899.998, 'low': 6771.42, 'close': 6852.444, 'volume': 15684575377.0, 'amount': 15414
2393679.33, 'time': 1587970803}
{'code': '399006', 'name': '创业板指', 'open': 2011.158, 'high': 2035.889, 'low': 1995.505, 'close': 2018.666, 'volume': 8452065145.0, 'amount': 10811
4103217.62, 'time': 1587970803}
{'code': '000003', 'name': 'B股指数', 'open': 211.9362, 'high': 212.6186, 'low': 210.5896, 'close': 210.7212, 'volume': 133377.0, 'amount': 53167014.
0, 'time': 1587970920}
{'code': '000063', 'name': '中兴通讯', 'open': 39.5, 'high': 39.59, 'low': 38.79, 'close': 38.85, 'volume': 58163720.0, 'amount': 2273854307.2, 'time'
: 1587970803}
{'code': '600036', 'name': '招商银行', 'open': 32.75, 'high': 33.98, 'low': 32.72, 'close': 33.58, 'volume': 65341471.0, 'amount': 2197523211.0, 'time
': 1587970800}
{'code': '600519', 'name': '贵州茅台', 'open': 1257.0, 'high': 1278.17, 'low': 1250.96, 'close': 1276.0, 'volume': 2590391.0, 'amount': 3284513346.0,
'time': 1587970800}
{'code': '300431', 'name': '暴风集团', 'open': 2.15, 'high': 2.26, 'low': 2.04, 'close': 2.09, 'volume': 17026817.0, 'amount': 36432934.05, 'time': 15
87970803}
{'code': '600050', 'name': '中国联通', 'open': 5.17, 'high': 5.21, 'low': 5.15, 'close': 5.18, 'volume': 67497600.0, 'amount': 350029689.0, 'time': 15
87970800}
{'code': '900925', 'name': '机电B股', 'open': 1.13, 'high': 1.142, 'low': 1.123, 'close': 1.126, 'volume': 64000.0, 'amount': 72271.0, 'time': 158797
0800}
{'code': '000063', 'name': '中兴通讯', 'open': 39.5, 'high': 39.59, 'low': 38.79, 'close': 38.85, 'volume': 58163720.0, 'amount': 2273854307.2, 'time'
: 1587970803}
{'code': '200026', 'name': '飞亚达B', 'open': 5.58, 'high': 5.58, 'low': 5.48, 'close': 5.48, 'volume': 40500.0, 'amount': 223230.0, 'time': 15879708
03}
{'code': '002097', 'name': '山河智能', 'open': 6.13, 'high': 6.34, 'low': 5.98, 'close': 6.3, 'volume': 26192743.0, 'amount': 162607704.23, 'time': 15
87970803}
{'code': '300003', 'name': '乐普医疗', 'open': 40.0, 'high': 40.35, 'low': 38.85, 'close': 39.44, 'volume': 12253411.0, 'amount': 484446397.11, 'time'
: 1587970803}
{'code': '871003', 'name': '卓力昕', 'open': 0.0, 'high': 0.0, 'low': 0.0, 'close': 0.0, 'volume': 0.0, 'amount': 0.0, 'time': 1587972616}
{'code': '688029', 'name': '南微医学', 'open': 169.1, 'high': 185.0, 'low': 168.97, 'close': 180.25, 'volume': 1307253.0, 'amount': 233225897.0, 'time
': 1587972599}
{'code': 'gb_dji', 'name': '道琼斯', 'open': 23628.2402, 'high': 23826.0, 'low': 23417.6797, 'close': 23775.2695, 'volume': 379306822.0, 'amount': Non
e, 'time': 1587763063}
{'code': 'gb_ixic', 'name': '纳斯达克', 'open': 8530.08, 'high': 8642.93, 'low': 8464.42, 'close': 8634.52, 'volume': 3401803941.0, 'amount': None, 't
ime': 1587778640}
{'code': 'gb_inx', 'name': '标普500指数', 'open': 2812.6399, 'high': 2842.71, 'low': 2791.76, 'close': 2836.74, 'volume': 2882793416.0, 'amount': None
, 'time': 1587763063}
{'code': 'rt_hkHSI', 'name': '恒生指数', 'open': 24030.66, 'high': 24318.811, 'low': 24030.541, 'close': 24280.141, 'volume': None, 'amount': 87119345
.79, 'time': 1587974908}

请按任意键继续. . .

你可能感兴趣的:(Python3)