MarkTechPost@AI 02月26日
FinData Explorer: A Step-by-Step Tutorial Using BeautifulSoup, yfinance, matplotlib, ipywidgets, and fpdf for Financial Data Extraction, Interactive Visualization, and Dynamic PDF Report Generation
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文详细介绍了如何使用Python在Google Colab上构建一个高级金融数据报告工具。该工具集成了多个Python库,包括用于网络数据抓取的BeautifulSoup、用于获取历史股票数据的yfinance、用于可视化趋势的matplotlib,以及用于创建交互式UI的ipywidgets。此外,还演示了如何使用FPDF生成动态PDF报告。通过本教程,读者可以学习到如何从网页上抓取实时金融数据、检索历史股票数据,并以用户友好的方式呈现这些数据。

💡**安装必要库**:首先,需要安装fpdf(用于生成PDF报告)、beautifulsoup4(用于网页抓取)、yfinance(用于检索历史金融数据)和ipywidgets(用于在notebook中创建交互式UI元素)。

📈**数据获取与可视化**:利用yfinance获取股票数据,并使用matplotlib绘制历史收盘价图表。图表清晰展示了指定日期范围内股票价格的波动情况,为后续报告生成提供数据基础。

📄**PDF报告生成**:使用FPDF库创建PDF报告,将股票代码、日期范围、当前价格以及历史收盘价图表嵌入到报告中。用户可以通过下载链接获取生成的PDF报告,方便数据分析和共享。

🎛️**交互式用户界面**:通过ipywidgets创建交互式用户界面,包括股票代码输入框、日期选择器和报告生成按钮。用户可以自定义股票代码和日期范围,从而生成个性化的金融报告。

In this tutorial, we will guide you through building an advanced financial data reporting tool on Google Colab by combining multiple Python libraries. You’ll learn how to scrape live financial data from web pages, retrieve historical stock data using yfinance, and visualize trends with matplotlib. Also, the tutorial demonstrates how to integrate an interactive UI using ipywidgets, culminating in a dynamic PDF report generated with FPDF.

!pip install fpdf beautifulsoup4 yfinance ipywidgets

First, we install the necessary libraries for our project: fpdf for generating PDF reports, beautifulsoup4 for web scraping, yfinance for retrieving historical financial data, and ipywidgets for creating interactive UI elements in the notebook.

import requestsfrom bs4 import BeautifulSoupfrom fpdf import FPDFimport yfinance as yfimport matplotlib.pyplot as pltimport ipywidgets as widgetsfrom IPython.display import display, FileLink

Here, we import a range of libraries to build a comprehensive financial data tool.

def generate_report(b):    symbol = symbol_text.value.upper().strip()    start_date = start_date_picker.value    end_date = end_date_picker.value       output_area.clear_output()  # Clear previous outputs       if not (symbol and start_date and end_date):        with output_area:            print("Please provide valid inputs for stock symbol and both dates.")        return       with output_area:        print(f"Generating report for {symbol} from {start_date} to {end_date}...")       # 1. Retrieve current price using yfinance    try:        stock = yf.Ticker(symbol)        current_price = stock.info.get('regularMarketPrice', 'N/A')    except Exception as e:        current_price = "Error retrieving price"        with output_area:            print("Error retrieving current price:", e)       # 2. Fetch historical data using yfinance    try:        hist = stock.history(start=start_date, end=end_date)    except Exception as e:        hist = None        with output_area:            print("Error fetching historical data:", e)       # 3. Plot historical closing prices    if hist is not None and not hist.empty:        plt.figure(figsize=(10, 5))        plt.plot(hist.index, hist['Close'], marker='o', linestyle='-', label="Close Price")        plt.title(f"{symbol} Historical Closing Prices")        plt.xlabel("Date")        plt.ylabel("Close Price (USD)")        plt.grid(True)        plt.xticks(rotation=45)        plt.tight_layout()        graph_filename = "graph.png"        plt.savefig(graph_filename)        plt.show()    else:        graph_filename = None        with output_area:            print("No historical data available for the selected date range.")       # 4. Create a PDF report using FPDF    pdf = FPDF()    pdf.add_page()    pdf.set_font("Arial", "B", 16)    pdf.cell(0, 10, f"Financial Report for {symbol}", ln=True, align="C")    pdf.ln(10)       pdf.set_font("Arial", size=12)    pdf.cell(0, 10, f"Current Price: {current_price}", ln=True)    pdf.cell(0, 10, f"Date Range: {start_date} to {end_date}", ln=True)    pdf.ln(10)       if graph_filename:        pdf.cell(0, 10, "Historical Closing Prices:", ln=True)        # Adjust the image width to fit the page layout        pdf.image(graph_filename, w=180)       pdf_filename = "financial_report.pdf"    pdf.output(pdf_filename)       # 5. Display the download link for the PDF report    with output_area:        print(f"PDF Report generated: {pdf_filename}")        display(FileLink(pdf_filename))

With the above function, we retrieve user inputs for the stock symbol and date range, then scrape the current financial data from Yahoo Finance while fetching historical data via yfinance. It plots the historical closing prices using matplotlib, generates a PDF report embedding the scraped data and the graph using FPDF, and finally displays a download link for the PDF report.

# Create UI widgetssymbol_text = widgets.Text(    value="AAPL",    description="Stock Symbol:",    placeholder="e.g., AAPL")start_date_picker = widgets.DatePicker(    description='Start Date')end_date_picker = widgets.DatePicker(    description='End Date')generate_button = widgets.Button(    description="Generate Report",    button_style='success')output_area = widgets.Output()generate_button.on_click(generate_report)display(widgets.VBox([symbol_text, start_date_picker, end_date_picker, generate_button, output_area]))

Finally, this code block sets up an interactive user interface using ipywidgets. It creates input fields for a stock symbol, date pickers for a start and end date, and a button to trigger the report generation. The UI elements are then organized vertically using a VBox layout, and an output area is provided to display feedback and the generated PDF download link.

Output and PDF Sample

In conclusion, by following this tutorial, you have successfully integrated web scraping, data analysis, interactive UI design, and PDF report generation into a single Google Colab notebook. This step-by-step process illustrates how to harness the power of Python’s diverse libraries to create a robust, user-friendly financial data tool.


Here is the Colab Notebook for the above project. Also, don’t forget to follow us on Twitter and join our Telegram Channel and LinkedIn Group. Don’t Forget to join our 80k+ ML SubReddit.

Recommended Read- LG AI Research Releases NEXUS: An Advanced System Integrating Agent AI System and Data Compliance Standards to Address Legal Concerns in AI Datasets

The post FinData Explorer: A Step-by-Step Tutorial Using BeautifulSoup, yfinance, matplotlib, ipywidgets, and fpdf for Financial Data Extraction, Interactive Visualization, and Dynamic PDF Report Generation appeared first on MarkTechPost.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

金融数据 Python 数据报告 Google Colab ipywidgets
相关文章