1.爬取沪深300股票的数据(便于更新)

#-----------------------------从网页上爬取沪深300的数据-------------------------------
import re
import requests
import openpyxl
from bs4 import BeautifulSoup

'''
获取网页内容
'''
def geturlcontent(url):   #可以多加一个参数 code
    try:
        headers = {
            'cookie': #自己添加
            'user-agent':# 自己添加}
        r=requests.get(url,headers=headers)
        r.raise_for_status()
        r.encoding=r.apparent_encoding  ##可以手工添加
        return r.text
    except:
        print("网页读取错误")

'''
分析网页内容
'''
def getstocklist(stocklist,html):
    soup=BeautifulSoup(html,"html.parser")
    td=soup.find_all("td")
    for i in td:
        try:
            href=i.string
            index=re.findall(r"^[0,3,6]\d{5}",href)[0]
            stockname=soup.find("a",attrs={"href":"gpdm.asp?gpdm="+index}).string
            stocklist.append((stockname,index))
        except:
            continue



#----------------2.将沪深300数据放入数据-----------------------------------
import pymysql

db=pymysql.connect(host= # 自己添加,
                   user= # 自己添加,
                   passwd= # 自己添加,
                   db="stock",
                   charset="utf8")
cursor=db.cursor()

'''
创建数据表
'''
def createTable():
    try:
        sql = '''
            create table hs300(
            id INT auto_increment PRIMARY KEY, 
            stock_name char(20), 
            stock_code char(20)
            )
        '''
        cursor.execute(sql)
        db.commit()
    except Exception as e:
        print(e)

'''
保存数据到MYSQL
'''
def savestocklist(list):
    sql="insert into hs300(stock_name,stock_code) values(%s,%s)"
    try:
        cursor.executemany(sql,list)
        db.commit()
    except:
        db.rollback()
    db.close()



def main():
    list=[]
    html=geturlcontent("http://www.shdjt.com/flsort.asp?lb=993505")
    getstocklist(list,html)
    createTable()
    savestocklist(list)

main()

2. 根据沪深300的数据创建单独的数据表

'''
读取数据表
'''
def gainTable(stocklist):
    sql="select * from hs300"
    try:
        cursor.execute(sql)
        results=cursor.fetchall()
        for i in results:
            stocklist.append(i[1]+"-"+i[2])
    except:
        print("Error: unable to fetch data")

'''
创建数据表
'''
def createTable(stocklist):
    for i in stocklist:
        try:
            sql = '''
                create table `%s`(
                id INT auto_increment PRIMARY KEY, 
                stock_time char(20), 
                stock_price_now float(20),
                stock_price_high float(20),
                stock_price_low float(20),
                stock_money float(20),
                stock_turn float(20),
                kdj_k float(20),
                kdj_d float(20),
                kdj_j float(20),
                boll_up float(20),
                boll_mid float(20),
                boll_low float(20)
                )
            '''%(i)
            cursor.execute(sql)
            db.commit()
        except Exception as e:
            print(e)

def main():
    list=[]
    gainTable(list)
    createTable(list)

main()

3.基于AKSHARE库将历史数据保存到表中

import akshare as ak
import numpy as np
from stock_index import *
'''
读取数据表
'''
def gainTable(stocklist):
    sql="select * from hs300"
    try:
        cursor.execute(sql)
        results=cursor.fetchall()
        for i in results:
            stocklist.append(i[1]+"-"+i[2])
    except:
        print("Error: unable to fetch data")

def gain_ak_info(stocklist):
    for i in stocklist:
        stockcode=i.split("-")[1]
        stock_info=ak.stock_zh_a_hist(symbol=stockcode,start_date="20180628",end_date="20210628",adjust="qfq")
        stock_matrix=np.array(stock_info.values)
        stock_analy_matrix=np.delete(stock_matrix,[1,5,7,8,9],axis=1)  #剔除ak中不需要的数据的矩阵
        k,d,j=KDJ(stock_analy_matrix[:,1].tolist(),stock_analy_matrix[:,2].tolist(),stock_analy_matrix[:,3].tolist())
        k=np.array(k)
        k=k.reshape(k.shape[0],1)
        d = np.array(d)
        d = d.reshape(d.shape[0], 1)
        j = np.array(j)
        j = j.reshape(j.shape[0], 1)
        ## BOLL
        upper,mid,low=BOLL(stock_analy_matrix[:,1].tolist())
        upper=np.array(upper)
        upper=upper.reshape(upper.shape[0],1)
        mid = np.array(mid)
        mid = mid.reshape(mid.shape[0], 1)
        low = np.array(low)
        low = low.reshape(low.shape[0], 1)
        ## 总矩阵
        total_matrix=np.append(stock_analy_matrix,k,axis=1)
        total_matrix= np.append(total_matrix, d, axis=1)
        total_matrix= np.append(total_matrix, j, axis=1)
        total_matrix= np.append(total_matrix, upper, axis=1)
        total_matrix= np.append(total_matrix, mid, axis=1)
        total_matrix= np.append(total_matrix, low, axis=1)
        data_matrix=total_matrix[:,0]
        data_list=data_matrix.tolist()
        old_matrix=np.delete(total_matrix,0,1)
        old_matrix = np.array(old_matrix, dtype=float)
        new_matrix = np.nan_to_num(old_matrix)
        total_list=new_matrix.tolist()
        vlist=[]
        for j in range(len(total_list)):
            vlist.append((data_list[j],total_list[j][0],total_list[j][1],total_list[j][2],total_list[j][3],total_list[j][4],total_list[j][5],total_list[j][6],total_list[j][7],total_list[j][8],total_list[j][9],total_list[j][10]))
        savestocklist(i,vlist)

'''
保存数据到MYSQL
'''
def savestocklist(code,list):
    sql = "insert into `%s`(stock_time,stock_price_now,stock_price_high,stock_price_low,stock_money,stock_turn,kdj_k,kdj_d,kdj_j,boll_up,boll_mid,boll_low)" %(code) + " values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    cursor.executemany(sql,list)
    db.commit()


#----------------3.main-----------------
def main():
    stocklist=[]
    gainTable(stocklist)
    gain_ak_info(stocklist)
    db.close()

main()

4.分析数据并将结果保存到excel中

这里的选股条件主要是:BOLL和KDJ指标,换手率以及成交额

'''
获取表名
'''
def gainTable(stocklist):
    sql="select * from hs300"
    try:
        cursor.execute(sql)
        results=cursor.fetchall()
        for i in results:
            stocklist.append(i[1]+"-"+i[2])
    except:
        print("Error: unable to fetch data")


'''
分析结果保存到excel
'''
def savestock(analysis_list):
    writebook=openpyxl.load_workbook("沪深300分析_kdj_boll_汇总.xlsx")
    writesheet=writebook.create_sheet("沪深300分析汇总")
    writesheet.cell(1,1).value="序列"
    writesheet.cell(1,2).value="股票代码"
    writesheet.cell(1,3).value="日期"
    writesheet.cell(1,4).value="收盘价"
    writesheet.cell(1,5).value="隔1天"
    writesheet.cell(1,6).value="隔3天"
    writesheet.cell(1,7).value="隔15天"
    writesheet.cell(1,8).value ="隔30天"
    row=2
    for list in analysis_list:
        writesheet.cell(row, 1).value = list[0]
        writesheet.cell(row, 2).value = list[1]
        writesheet.cell(row, 3).value = list[2]
        writesheet.cell(row, 4).value = list[3]
        writesheet.cell(row, 5).value = list[4]
        writesheet.cell(row, 6).value = list[5]
        writesheet.cell(row, 7).value = list[6]
        writesheet.cell(row, 8).value = list[7]
        row=row+1
    writebook.save("沪深300分析_kdj_boll_汇总.xlsx")
    writebook.close()

'''
分析数据
'''
def analysis(stocklist):
    savelist = []
    for stockcode in stocklist:
        sql="select * from `%s`"%(stockcode)
        # try:
        cursor.execute(sql)
        results=cursor.fetchall()
        count=1
        for i in results:
            if i[9]<=-10 and i[12]>i[2]:  # 指标判断
                if i[5]>=3000000000 and i[6]>=2:    # 金额和换手率判断
                    if i[0]+30 >=len(results):
                        continue
                    else:
                        savelist.append([count,stockcode,i[1],i[2],results[i[0]][2],results[i[0]+2][2],results[i[0]+14][2],results[i[0]+29][2]])
                        count=count+1
    savestock(savelist)
        # except:
        #     print("Error: unable to fetch data")


def main():
    stocklist=[]
    gainTable(stocklist)
    analysis(stocklist)

main()

结果

以这种方式选出来的个股,观察1、3、15、30天后的结果,根据excel分析结果如下:
在这里插入图片描述
结果并不是很理想。。。

展望

但问题不大,过程总是艰辛的,无非是调整选股思路
也希望对量化股票感兴趣的朋友可以一起交流交流