爬虫结果图
数据展示图
五、程序代码解析(源代码下载地址及数据库文章底部会提供):
1、把所有股票的基本信息都保存在一个mysql数据库中gp.sql,总过三千六百多条,如下图:
2、获取股票当天所有的数据get_gp_detail.py:
import pymysql
import numpy as np
import sys
import json
import urllib.request
import urllib
import os
import time
#连接数据库
db = pymysql.connect(host='127.0.0.1',user='root',password='root',db='gp_db',port=3306)
#获取cursor
cursor = db.cursor()# 使用 execute() 方法执行 SQL,如果表存在则删除
sql = "select * from gp"
cursor.execute(sql)
print("SELECT OK")
#all_gp = cursor.fetchmany(1)
all_gp = cursor.fetchall() #从数据库中获取所有股票的基本信息数据
arr = np.array(all_gp) #转化为numpy数据格式
now = int(time.time())
#转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
timeStruct = time.localtime(now)
strTime = time.strftime("%Y-%m-%d", timeStruct)
gp_count = 1 #股票当天所有数据的保存编号
def mkdir(path): #股票保存路径函数
folder = os.path.exists(path)
if not folder: #判断是否存在文件夹如果不存在则创建为文件夹
os.makedirs(path) #makedirs 创建文件时如果路径不存在会创建这个路径
print(path)
def getData(url): #函数——从接口中获取单只股票当天每分钟的数据
content = ""
try: #网络会偶发出现奔溃情况,为了保证不中断和保证数据齐全