import http.server
import socketserver
PORT = 8001
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("127.0.0.1", PORT), Handler) as httpd:
print(f"Serving at port {PORT}")
httpd.serve_forever()
echarts 图表代码 (收入与与利润)
收入和成本图表
收入和成本图表
echarts 图表代码 (各个年份对应的季节利润)
ECharts Line Chart
echarts 图表代码 (预测下一年份数据图表)
预测下一年利润
预测下一年利润
可视化大屏图表
可视化大屏
可视化大屏
关于该数据爬虫代码
# sql = " SELECT address-price as price FROM `test` WHERE YEAR(`title`) >= YEAR(CURDATE()) - 5 AND YEAR(`title`) <= YEAR(CURDATE()) - 1 GROUP by title;"
# sql = " SELECT sum(address) as shouru, sum(price) as chengben FROM `test` WHERE YEAR(`title`) >= YEAR(CURDATE()) - 5 AND YEAR(`title`) <= YEAR(CURDATE()) - 1 GROUP by LEFT(`title`, 4);;"
import datetime
import requests
import openpyxl
import pymysql
import time
import re
import json
import pandas as pd
from matplotlib.widgets import Button
from lxml import etree
from dbutils.pooled_db import PooledDB
import matplotlib.pyplot as plt
pool = PooledDB(pymysql, maxconnections=5, host='192.168.14.93', user='root', password='abc123', database='res_info',
charset='utf8')
def get_result(year):
url = 'https://money.finance.sina.com.cn/corp/go.php/vFD_ProfitStatement/stockid/002279/ctrl/{}/displaytype/4.phtml'.format(year)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.188'}
response = requests.get(url, headers=headers)
# 所需网页内容
res = response.text
# 初始化xpath
tree = etree.HTML(res)
# descendant-or-self::* 提取后代所有元素
title = tree.xpath('//table[@id="ProfitStatementNewTable0"]/tbody/tr/td[@style="font-weight:bold" or @style="padding-left:30px"]/descendant-or-self::*/text()')
# 取出所有的数据
elements = (tree.xpath('//table[@id="ProfitStatementNewTable0"]/tbody/tr/td[@style="text-align:right;" or (not(@style))]//text()'))[1:]
res = elements[4:]
# 将前四个字段截取,之后每个字段的第一位都与第一个字段对应 第二位都与第二位字段对应以此类推 总共三个字段
res_time = elements[0:4]
# 冬季
winter = [res[i] for i in range(0, len(res), 4)]
winter.insert(0,res_time[0])
# 秋季
autumn = [res[i] for i in range(1, len(res), 4)]
autumn.insert(0,res_time[1])
# 夏季
summer = [res[i] for i in range(2, len(res), 4)]
summer.insert(0,res_time[2])
# 春季
spring = [res[i] for i in range(3, len(res), 4)]
spring.insert(0,res_time[3])
# 创建一个字典,用于存储数据
data = [{'title':title},{'resultlist':[spring,summer,autumn,winter]},]
# 指定要保存的文件路径和文件名
output_file = "output.json"
# 将数据写入JSON文件
with open(output_file, "w",encoding='utf-8') as f:
json.dump(data, f, indent=4,ensure_ascii=False)
# 将数据写入数据库
def sql_insert(res):
for i in res:
conn = pool.connection()
sql = 'INSERT INTO `test` (`title`, `address`, `content`, `price`, `image`,`Column1`,`Column2`,`Column3`,`Column4`,`Column5`,`Column6`,`Column7`,`Column8`,`Column9`,`Column10`,' \
'`Column11`,`Column12`,`Column13`,`Column14`,`Column15`,`Column16`,`Column17`,`Column18`,`Column19`,`Column20`,`Column21`,`Column22`,`Column23`,`Column24`,`Column25`) ' \
'VALUES ( "%s", "%s", "%s", "%s","%s", "%s", "%s", "%s", "%s","%s", "%s", "%s", "%s", "%s","%s",' \
' "%s", "%s", "%s", "%s","%s", "%s", "%s", "%s", "%s","%s", "%s", "%s", "%s", "%s","%s")' % (
i[0],i[1],i[2],i[3],i[4],i[5],i[6],i[7],
i[8],i[9],i[10],i[11],i[12],i[13],i[14],i[15],
i[16],i[17],i[18],i[19],i[20],i[21],i[22],i[23],
i[24],i[25],i[26],i[27],i[28],i[29]
)
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
conn.close()
sql_insert([spring,summer,autumn,winter])
try:
get_result(2018)
get_result(2019)
get_result(2020)
get_result(2021)
get_result(2022)
except Exception as f:
print('错误为:',f)
AIchatOS (binjie.fun)