第一步:下载相关安装包
pip install streamlit pip install streamlit-echarts pip install pymysql
第二步:下载文本编辑器node++(互联网下载)
第三步:爬取数据
爬取数据方式1:用pandas的方式爬取数据并生成柱状折线图
import pandas as pd #import pyecharts.options as opts #from pyecharts.charts import Bar, Line import matplotlib.pyplot as plt result=pd.DataFrame() for year in range(2018,2023): url = f"https://money.finance.sina.com.cn/corp/go.php/vFD_ProfitStatement/stockid/002279/ctrl/{year}/displaytype/4.phtml" df=pd.read_html(url) table=df[13] table.drop("久其软件(002279) 利润表单位:万元.5", axis=1, inplace=True) fistrow=table.iloc[0].tolist() table.columns=fistrow table.drop(index=0, inplace=True) table.drop(index=1, inplace=True) table.set_index('报表日期', inplace=True) result=pd.concat([result,table],axis=1) data_year=result[['2018-12-31','2019-12-31','2020-12-31','2021-12-31','2022-12-31']] zyysr=data_year.iloc[0].tolist() zyycb=data_year.iloc[2].tolist() zyysr=list(map(float,zyysr[0:5])) zyycb=list(map(float,zyycb[0:5])) x_data=['2018','2019','2020','2021','2022'] plt.bar(x_data, zyycb,color='red') #绘制柱状图 plt.plot(x_data, zyysr) #绘制折线图 plt.show() #显示图形
爬取数据方式2:用BeautifuiSoup的方式爬取数据并生成柱状折线图
from bs4 import BeautifulSoup import pandas as pd import requests import matplotlib.pyplot as plt df_all=pd.DataFrame(columns=["项目","年度","四季度","三季度","二季度","一季度"]) for year in range(2018,2023): url=f"https://money.finance.sina.com.cn/corp/go.php/vFD_ProfitStatement/stockid/002279/ctrl/{year}/displaytype/4.phtml" html=requests.get(url) soup = BeautifulSoup(html.text, "lxml") result=[] for row in soup.find("table", id="ProfitStatementNewTable0").find_all("tr"): col = row.find_all("td") if len(col)==5: if col[0].text!='报表日期': text0=col[0].text text1=col[1].text.replace(',','') text2=col[2].text.replace(',','') text3=col[3].text.replace(',','') text4=col[4].text.replace(',','') result.append([text0,year,text1,text2,text3,text4]) df=pd.DataFrame(result) df.columns=["项目","年度","四季度","三季度","二季度","一季度"] df_all=pd.concat([df_all, df], axis=0, ignore_index=True) yyzsr=df_all['四季度'][df_all['项目']=='一、营业总收入'].to_list() yyzsr1=[] for je in yyzsr: je1=float(je) yyzsr1.append(je1) yyzcb=df_all['四季度'][df_all['项目']=='二、营业总成本'].to_list() yyzcb1=[] for je in yyzcb: je1=float(je) yyzcb1.append(je1) x_data=['2018','2019','2020','2021','2022'] plt.bar(x_data, yyzcb1,color='red') #绘制柱状图 plt.plot(x_data, yyzsr1) #绘制折线图 plt.show() #显示图形
第四步:存入数据库
生成CSV
pandas生成CSV或者json的方式:
pd.to_csv() pd.to_json()
第五步:生成大屏幕显示
生成.py代码
import pymysql import streamlit as st import pandas as pd st.set_page_config(layout='wide') conn=pymysql.connect(host='node2', port=3306, user='bigdata', password='bigdata123', database='shtd_store', charset='utf8') c11,c12,c13=st.columns([3,1,3]) with c12: st.header("大屏展示") c21,c22,c23=st.columns(3) with c21: year=st.selectbox("选择年度",range(2018,2023)) sql=f""" select 一季度,cast((二季度-一季度) as DECIMAL(10,2)) as 二季度, cast((三季度-二季度) as DECIMAL(10,2)) as 三季度,ROUND((四季度-三季度),2) as 四季度 from jqcw1 where 项目='三、营业利润' and 年度={year} """ df=pd.read_sql(sql,conn) from streamlit_echarts import st_echarts options = { "xAxis": { "type": "category", "data": ['一','二','三','四'], }, "yAxis": {"type": "value"}, "series": [ {"data": df.iloc[0].to_list(), "type": "line"} ], } st_echarts(options=options) with c22: options1 = { "xAxis": { "type": "category", "data": ['一','二','三','四'], }, "yAxis": {"type": "value"}, "series": [ {"data": df.iloc[0].to_list(), "type": "bar"} ], } st_echarts(options=options1)
运行
streamlit run .py
预测算法
https://www.php.cn/faq/555711.html