• Python实现股票信息查询


    目前两个常用的股票信息CPI
    腾讯行情CTPAPI接口源码
    新浪行情CTPAPI
    使用requests模块爬取股票信息,这里以查询股票市值为例。

    一、根据股票名称查询股票代码

    在python文件夹下设置两个表格GPLIST.xlsx,其中是A股全部代码和股票名称,query_stock_names.xlsx`是要查询的股票名称

    stockfinder-3.py

    import openpyxl  
      
    def create_stock_dict(file_path):  
        stock_dict = {}  
        wb = openpyxl.load_workbook(filename=file_path)  
        ws = wb.active  
        for row in ws.iter_rows(values_only=True):  
            stock_dict[row[1]] = row[0]  
        return stock_dict  
      
    def get_stock_code(stock_name, stock_dict):  
        return stock_dict.get(stock_name, "Not Found")  
      
    def main():  
        stock_dict = create_stock_dict('GPLIST.xlsx')  
      
        wb = openpyxl.load_workbook(filename='query_stock_names-1.xlsx')  
        ws = wb.active  
        for row in ws.iter_rows(min_row=1, min_col=1, max_col=1, values_only=True):  
            stock_name = row[0]  
            stock_code = get_stock_code(stock_name, stock_dict)  
            ws.cell(row=ws.max_row+1, column=2, value=stock_code)  
      
        wb.save('query_stock_names-1.xlsx')  
      
    if __name__ == "__main__":  
        main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    二、股票市值查询

    1.单个股票市值查询
    lianghua-2.py

    import requests  
    from dateutil import parser  
    from datetime import datetime,time  
    from time import sleep  
      
      
    def get_tick(stock_code):  
        if stock_code[0] in ['5','6']:  
            prefix = 'sh'  
        elif stock_code[0] in ['0','3']:  
            prefix = 'sz'  
        else:  
            raise Exception('prefix')  
        page = requests.get('http://qt.gtimg.cn/q='+prefix+stock_code)  
        stock_info = page.text  
        stock_info = stock_info.split('~')  
        open_ = float(stock_info[5])# string  
        high = float(stock_info[33])  
        low = float(stock_info[34])  
        close = float(stock_info[3])  
        shizhi = float(stock_info[45])  
        trade_datetime = parser.parse(stock_info[30]) #将string格式解析成日期格式  
        return shizhi  
      
      
    if __name__ == "__main__":  
        stock_c = '601088'  
        print(get_tick(stock_c))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    2.批量股票市值查询
    stockshizhiquery-2.py

    import requests  
    import openpyxl  
      
    def get_tick(stock_code):  
        if stock_code[0] in ['5', '6']:  
            prefix = 'sh'  
        elif stock_code[0] in ['0', '3']:  
            prefix = 'sz'  
        else:  
            raise Exception('Invalid stock code prefix')  
        page = requests.get('http://qt.gtimg.cn/q=' + prefix + stock_code)  
        stock_info = page.text  
        stock_info = stock_info.split('~')  
        shizhi = float(stock_info[45]) if stock_info[45] else 0.0  # 如果市值为空,则设为0  
        return shizhi  
      
      
        # 打开 Excel 文件  
    workbook = openpyxl.load_workbook('query_stock_names-1.xlsx')  
      
        # 获取第一个工作表  
    sheet = workbook.active  
      
        # 获取每支股票的市值,并将其追加到第三列  
    for row in sheet.iter_rows(min_row=1, values_only=True):  # 假设股票代码在第一列,从第二行开始读取数据  
      
            stock_code = str(row[0])  
            shizhi = get_tick(stock_code)  
            # sheet.append([shizhi])  
            sheet.cell(row=sheet.max_row + 1, column=3, value=shizhi)  
      
      
    workbook.save('query_stock_names-1_updated.xlsx')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    前端面试宝典React篇02 为什么 React 要用 JSX?
    低代码高拓展性依托何物?一起来探究一番!
    stm32 iap sd卡升级
    yolact ncnn保姆级源码解读(结合paper)
    GBase 8c 管理平台(一)-部署配置
    哈希树讲解
    serialVersionUID、transient关键字、Properties作为Map集合的使用、特有方法及和IO流结合的方法
    普通学校,普通背景,普通公司,不普通总结。
    word批量修改表格样式
    【echarts】04、echarts+vue2 - 折线图
  • 原文地址:https://blog.csdn.net/wudi1107/article/details/136460693