多线程学习

导包:

import requests
import json
import pandas as pd
import threading
import akshare as ak
from datetime import datetime
from tqdm import tqdm
from sqlalchemy import create_engine

功能:

#写入数据
engine = create_engine('mysql+pymysql://root:888@localhost:3306/stock_2')


now_date_str=str(datetime.now())[:10]
now_date=''.join(str(datetime.now())[:10].split('-'))
def get_all_stock_code():
    '''
    获取全部股票代码
    '''
    df=ak.stock_individual_fund_flow_rank(indicator='今日')
    df1=df[['代码','名称']]
    #st
    st=ak.stock_zh_a_st_em()[['代码','名称']]
    #退市
    stop=ak.stock_zh_a_stop_em()[['代码','名称']]
    #合并st和退市
    st_stop=pd.concat([st,stop],ignore_index=True)
    code=df1['代码'].tolist()
    name=df1['名称'].tolist()
    #删除st,stop
    for m,n in zip(st_stop['代码'].tolist(),st_stop['名称'].tolist()):
        try:
            m_index=code.index(m)
            n_index=name.index(n)
            del code[m_index]
            del name[n_index]
        except:
            pass
    data=pd.DataFrame({'代码':code,'名称':name})
    #删除北京交易所
    def select_data(x):
        if x[:3]=='688':
            return '是'
        else:
            return '无'
    data['北京交易所']=data['代码'].apply(select_data)
    #选择数据
    df2=data[data['北京交易所']=='无']
    del df2['北京交易所']
    return df2
all_code=get_all_stock_code()
code_dict=dict(zip(all_code['代码'].tolist(),all_code['名称'].tolist()))
code_list=all_code['代码'].tolist()
def get_stock_daily_em(stock='600031',start_date='20000101',end_date=now_date):
    '''
    获取股票日线数据
    '''
    if stock[0]=='6':
        stock='1.'+stock
    else:
        stock='0.'+stock
    url='http://push2his.eastmoney.com/api/qt/stock/kline/get?'
    params={
        'fields1':'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13',
        'fields2':'f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61',
        'beg':start_date,
        'end':'20500101',
        'ut':'fa5fd1943c7b386f172d6893dbfba10b',
        'rtntype':end_date,
        'secid':stock,
        'klt':'101',
        'fqt':'1',
        'cb':'jsonp1668432946680'
    }
    res=requests.get(url=url,params=params)
    text=res.text[19:len(res.text)-2]
    json_text=json.loads(text)
    df=pd.DataFrame(json_text['data']['klines'])
    df.columns=['数据']
    data_list=[]
    for i in df['数据']:
        data_list.append(i.split(','))
    data=pd.DataFrame(data_list)
    columns=['date','open','close','high','low','成交量','成交额','振幅','涨跌幅','涨跌额','换手率']
    data.columns=columns
    
    
    data.to_sql(name = code_dict[stock[2:]],con=engine,index=False,if_exists='replace')
    
                
                
    #data.to_csv(r'下载数据\{}.csv'.format(code_dict[stock[2:]]))
    print(code_dict[stock[2:]],'下载完成')
    return data

线程:

#多线程
def down_all_data():
     #线程池
    threading_list=[]
    for stock in code_list:
        threading_list.append(threading.Thread(target=get_stock_daily_em,args=(stock,)))
    #线程启动
    for down in threading_list:
        down.start()
    #join就是阻塞,主进程有join,主进程下面的代码一律不执行,直到进程执行完毕之后,再执行。
    for down in threading_list:
        down.join()

你可能感兴趣的:(量化,python,学习,python,开发语言)