当前位置: 首页 > news >正文

爬取基金收盘价并用pyecharts进行展现

爬取基金收盘价并用pyecharts进行展现

一、用到的第三方包

因为使用到了一些第三方的包,包还是比较大的如果直接从社区下载比较费劲,所以建议配置国内镜像源,这里以清华的镜像源为例。

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip config set global.trusted_host pypi.tuna.tsinghua.edu.cn

安装第三方包

pip install pandas bs4 pyecharts

二、数据获取

从网页爬取数据

import requests
from bs4 import BeautifulSoup
# 从url获取信息
response = requests.get('url地址')
# 解析HTML内容
soup = BeautifulSoup(response.text, 'html.parser')
soup.get_text()

1、从网页爬取每日最新数据

2、将数据追加存储到excel

# !/usr/bin/python
# -*-coding:utf-8 -*-
"""File        : spider.pyTime        : 2024/1/17 10:00Author      : 天选之子Email       : version     : python 3.10.11Description : 
"""
import datetime
import osimport numpy
import requests
from bs4 import BeautifulSoup
import pandasurl = "https://qt.gtimg.cn"
fund_code_list = ['sh510300', 'sz159995']
target_path = r'excel存储位置'
sheet_name = '存储的excel的sheet页'def get_fund_close_price(url_str):"""获取基金收盘价"""response = requests.get(url_str)soup = BeautifulSoup(response.text, 'html.parser')if soup is None:return '没找到'else:market = soup.get_text()return market.split('~')[4]def df_combiner(targetpath, dataframe, sheetname):"""将来源excel合并到目标excel:param targetpath::param dataframe::param sheetname::return: dataframe合并后的最终excel对象"""if not os.path.exists(targetpath):return Nonetar_data_frame = pandas.DataFrame(pandas.read_excel(targetpath, sheet_name=sheetname, keep_default_na=False))tar_date = set(numpy.array(tar_data_frame['日期']).tolist())to_date = set(numpy.array(dataframe['日期']).tolist())if to_date.issubset(tar_date):tar_df_all = tar_data_frameelse:if tar_data_frame is None:return dataframetar_df_all = pandas.concat([tar_data_frame, dataframe], ignore_index=True)return tar_df_alldef main():df_all = Nonefor fund_code in fund_code_list:fund_url = f'{url}/?q={fund_code}'result = get_fund_close_price(fund_url)print("该只基金的收盘价为:", result)print(datetime.datetime.now().strftime('%Y-%m-%d'))# 创建一个DataFramedata = {'日期': [datetime.datetime.now().strftime('%Y%m%d')], '基金代码': [fund_code], '收盘价': [result]}df = pandas.DataFrame(data)df_all = pandas.concat([df_all, df], ignore_index=True)target_df = df_combiner(target_path, df_all, sheet_name)excel_writer = pandas.ExcelWriter(target_path)target_df.to_excel(excel_writer, index=False, sheet_name='基金收盘价')excel_writer.close()if __name__ == '__main__':main()

三、展示

将获取的excel数据展示成折线图,这里使用的是pyecharts,不多废话直接上代码

# https://pyecharts.org/#/zh-cn/quickstart
import numpy
import pandas
from pyecharts import options as opts
from pyecharts.charts import Linetarget_path = r'excel位置'
sheet_name = '基金收盘价'
fund_code_list = ['sh510300', 'sz159995']
tar_data_frame = pandas.DataFrame(pandas.read_excel(target_path, sheet_name=sheet_name, keep_default_na=False))
data_date = list(set(numpy.array(tar_data_frame['日期']).tolist()))
sh510300_date = numpy.array(tar_data_frame.loc[tar_data_frame['基金代码'] == 'sh510300']['收盘价']).tolist()
sz159995_date = numpy.array(tar_data_frame.loc[tar_data_frame['基金代码'] == 'sz159995']['收盘价']).tolist()# 创建柱状图
bar_chart = Line()
bar_chart.add_xaxis(data_date)
title_opts = opts.TitleOpts(title="月度销售额折线图"),
bar_chart.add_yaxis(series_name='sh510300', linestyle_opts=opts.LineStyleOpts(color="red", width=2, is_show=True), itemstyle_opts=opts.ItemStyleOpts(color="red", border_width=1), is_smooth=True, y_axis=sh510300_date)
bar_chart.add_yaxis(series_name='sz159995_date', linestyle_opts=opts.LineStyleOpts(color="yellow", width=2, is_show=True), itemstyle_opts=opts.ItemStyleOpts(color="yellow", border_width=1), is_smooth=True, y_axis=sz159995_date)
bar_chart.set_global_opts(title_opts=opts.TitleOpts(title="基金收盘价格走势", is_show=True, pos_left='center'), xaxis_opts=opts.AxisOpts(name="月份"), yaxis_opts=opts.AxisOpts(name="收盘价(元)", min_=0, max_=10), legend_opts=opts.LegendOpts(pos_left='right'))# 渲染图表到 HTML 文件
bar_chart.render("基金收盘价.html")

展示结果如下:
在这里插入图片描述

http://www.lryc.cn/news/365932.html

相关文章:

  • 各平台对象存储
  • C# 中提取方法进行重构
  • 每天一个数据分析题(三百四十三)
  • 【leetcode--统计优美子数组】
  • 开源模型应用落地-LangChain高阶-LCEL-表达式语言(二)
  • shell脚本对编码和行尾符敏感吗
  • 神经网络----现有网络的下载和使用(vgg16)
  • Java 异常处理 - 自定义异常
  • Excel 交叉表的格转成列,行转成格
  • 【C++软件调试技术】什么是pdb文件?如何使用pdb文件?哪些工具需要使用pdb文件?
  • 如何搭建一台永久运行的个人服务器?
  • Ant Design+react 表单只读
  • 推荐系统三十六式学习笔记:原理篇.近邻推荐07|人以群分,你是什么人就看到什么世界
  • 要改进单例模式的实现以确保线程安全并优化性能,有几种常见的方法
  • k8s——Pod容器中的存储方式及PV、PVC
  • Java/Golang:活用interface,增加程序扩展性
  • SQL语句练习每日5题(四)
  • Java排序算法汇总篇,八种排序算法
  • R语言探索与分析20-北京市气温预测分析
  • 2024年安全现状报告
  • OV通配符ssl证书是什么
  • Selenium三种等待方式的使用!
  • websockets怎么工作的呢?
  • 栈 数组和链表实现
  • 如何备份和恢复华为手机?
  • 微波电路S参数测量实验方案
  • SpringTask Cron表达式
  • docker与docker-compose安装
  • 跨境反向海淘系统:业务流程解析与未来发展展望
  • Python语言字母:深度解析与魅力探索