请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3365818 新帖:25

获取申万官网申万行业历史行情数据

TRADE12发表于:5 月 10 日 05:26回复(1)

获取申万官网申万行业历史行情数据

#获取申万官网申万行业数据

#导入库
import numpy as np
import pandas as pd
import requests
import json
from datetime import timedelta,date


# 获取申万官网申万行业数据
# code:行业代码
# frequency:day/week/month
# start_date:None(表示最早日期)
# end_date:None(表示今天日期)
# fields:None(表示所有字段)
def get_sw_data(code=None,start_date=None,end_date=None,frequency='day',fields=None): 
    #headers
    header={
        'HOST':'www.swsindex.com',
        'Referer':'http://www.swsindex.com/idx0200.aspx?columnid=8838&type=Day',
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) \
    Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4482.400 QQBrowser/9.7.13001.400'
        }

    #传入参数
    param={
        'tablename':'V_Report',
        'key':'id',
        #页面序号,每页返回20条数据
        'p':'1',
        #查询语句,查询的代码、日期、数据类型
        "where":"swindexcode in ('801020') and   BargainDate>='2018-04-02' and  BargainDate<='2018-04-24' and type='Day'",
        #排序(swindexcode asc表示按照代码升序,BargainDate_1表示按照日期降序,_2表示按照升序)
        'orderby':'swindexcode asc,BargainDate_2',
        #返回的字段
        'fieldlist':'SwIndexCode,SwIndexName,BargainDate,OpenIndex,CloseIndex,MaxIndex,MinIndex,BargainAmount,BargainSum,Markup,TurnoverRate,\
    PE,PB,MeanPrice,BargainSumRate,NegotiablesShareSum,NegotiablesShareSum2,DP',
        'pagecount':'1',
        'timed':'1524497094532',
        }
    
    #数据表表头
    sw_columns_list=['SwIndexCode','SwIndexName','BargainDate','OpenIndex','CloseIndex','MaxIndex','MinIndex','BargainAmount','BargainSum',
         'Markup','TurnoverRate','PE','PB','MeanPrice','BargainSumRate','NegotiablesShareSum','NegotiablesShareSum2','DP']

    #数据类型(日、周、月)
    frequency_list=['day','week','month']

    #申万一级行业代码列表
    code_list=['801010','801020','801030','801040','801050','801080','801110','801120','801130','801140','801150','801160','801170','801180',
       '801200','801210','801230','801710','801720','801730','801740','801750','801760','801770','801780','801790','801880','801890']    
    
    #配置查询语句
    where="swindexcode in ("
    if code is None:
        #如果代码为空,则代码为代码列表
        code=str(code_list).replace('[','').replace(']','')
    else:    
        if type(code)==list:
            #如果代码为列表,判断代码是否在代码列表中
            if set(code).issubset(set(code_list)):
                code_str=str(code).replace('[','').replace(']','')
            else:
                code_str=str(code_list).replace('[','').replace(']','')
        if type(code)==str:
            if code in code_list:   
                code_str="'"+code+"'"
            else:    
                code_str=str(code_list).replace('[','').replace(']','')
    where+=code_str   
    
    #配置日期
    today_str=pd.datetime.today().strftime('%Y-%m-%d')
    if (start_date is None) or (start_date<'1999-12-30') or (start_date>today_str):
        start_date='1999-12-30'
    where+=") and BargainDate>='"     
    where+=start_date
    if (end_date is None) or (end_date>today_str) or (end_date<'1999-12-30'):
        end_date=today_str
    where+="' and BargainDate<='" 
    where+=end_date  
    
    #配置数据类型
    if not(frequency in frequency_list):  
        frequency='day'
    where+="' and type='"
    where+=frequency
    where+="'"
    param['where']=where 
    
    #配置字段
    columns=sw_columns_list
    fieldlist=str(sw_columns_list).replace(" ","").replace("'","").replace('[',"").replace(']',"")   
    if not(fields is None):
        if(set(fields).issubset(set(sw_columns_list))):  
            if not (['SwIndexCode','SwIndexName','BargainDate'] in fields):
                fields=['SwIndexCode','SwIndexName','BargainDate']+fields
        fieldlist=str(fields).replace(" ","").replace("'","").replace('[',"").replace(']',"") 
        columns=fields
    param['fieldlist']=fieldlist
    
    df=pd.DataFrame()
    #url
    url='http://www.swsindex.com/handler.aspx'
    #页面计数器
    page=1
    while True:
        #获取数据
        ret=requests.get(url,data=param,headers=header)
        if not (ret.ok is True):
            break
        #整理引号、日期格式    
        data=ret.text.replace("'", '"').replace(' 0:00:00','').replace('/','-')
        #解析数据
        data=json.loads(data).get('root')
        if len(data)==0:
            break
        #追加数据表    
        df=df.append(pd.DataFrame(data,columns=columns))
        #设置页面计数器
        page+=1
        param['p']=str(page)    
    if len(df)!=0:   
        df.BargainDate=pd.to_datetime(df.BargainDate,format='%Y-%m-%d')
        
    #返回数据
    return df 
df=get_sw_data('801010',start_date='2018-04-23')
df
SwIndexCode SwIndexName BargainDate OpenIndex CloseIndex MaxIndex MinIndex BargainAmount BargainSum Markup TurnoverRate PE PB MeanPrice BargainSumRate NegotiablesShareSum NegotiablesShareSum2 DP
0 801010 农林牧渔 2018-04-23 2660.97 2628.76 2661.68 2615.21 75182 687374 -1.33 1.0162 27.45 2.86 11.96 1.75 66565165.81 715754.47 1.46
1 801010 农林牧渔 2018-04-24 2629.01 2666.11 2668.55 2628.56 66003 647119 1.42 0.8922 27.95 2.87 12.20 1.43 67545648.66 726297.30 1.44
2 801010 农林牧渔 2018-04-25 2658.15 2652.06 2664.91 2649.23 60600 605600 -0.53 0.8178 27.78 2.85 12.08 1.44 67189712.38 722470.03 1.45
3 801010 农林牧渔 2018-04-26 2648.26 2614.21 2653.93 2610.00 73849 732824 -1.43 0.9965 27.33 2.81 11.86 1.73 66282357.87 712713.53 1.47
df=get_sw_data(code=['801010','801020'],start_date='2018-04-23',fields=['PE','PB'])
df
SwIndexCode SwIndexName BargainDate PE PB
0 801010 农林牧渔 2018-04-23 27.45 2.86
1 801010 农林牧渔 2018-04-24 27.95 2.87
2 801010 农林牧渔 2018-04-25 27.78 2.85
3 801010 农林牧渔 2018-04-26 27.33 2.81
4 801020 采掘 2018-04-23 16.79 1.38
5 801020 采掘 2018-04-24 16.85 1.40
6 801020 采掘 2018-04-25 16.91 1.39
7 801020 采掘 2018-04-26 16.97 1.36
df=get_sw_data('801010',start_date='2018-03-1',frequency='week')
df
SwIndexCode SwIndexName BargainDate OpenIndex CloseIndex MaxIndex MinIndex BargainAmount BargainSum Markup TurnoverRate PE PB MeanPrice BargainSumRate NegotiablesShareSum NegotiablesShareSum2 DP
0 801010 农林牧渔 2018-03-02 2727.50 2730.01 2736.61 2723.59 386007 3172591 0.63 0.9749 31.40 3.03 11.81 1.45 67991460.10 739037.61 1.56
1 801010 农林牧渔 2018-03-09 2746.99 2785.28 2786.60 2745.61 326770 2973270 2.02 0.9271 32.02 3.09 12.18 1.34 69377561.55 754103.93 1.53
2 801010 农林牧渔 2018-03-16 2759.08 2750.86 2771.38 2750.72 361394 3205744 -1.24 0.7277 31.59 3.05 12.10 1.34 68818359.97 748025.65 1.54
3 801010 农林牧渔 2018-03-23 2698.33 2741.01 2805.03 2691.86 496488 4330554 -0.36 3.3006 31.50 3.03 12.04 1.85 68909747.48 749018.99 1.54
4 801010 农林牧渔 2018-03-30 2713.72 2715.17 2717.83 2700.17 643216 5506492 -0.94 1.2378 31.00 3.01 12.18 2.22 68654502.68 746244.59 1.55
5 801010 农林牧渔 2018-04-13 2741.72 2722.86 2745.83 2718.02 517521 4917642 -2.58 0.9061 28.41 2.97 12.72 2.21 69145492.55 743499.92 1.41
6 801010 农林牧渔 2018-04-20 2691.73 2664.20 2696.91 2660.21 530601 5001051 -2.15 1.1773 27.88 2.89 12.25 2.14 67586558.04 726737.18 1.44
 

全部回复

0/140

量化课程

    移动端课程