欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > python抓取HTML页面数据+可视化数据分析(投资者数量趋势)

python抓取HTML页面数据+可视化数据分析(投资者数量趋势)

2025/4/22 8:57:49 来源:https://blog.csdn.net/weixin_43006743/article/details/147336200  浏览:    关键词:python抓取HTML页面数据+可视化数据分析(投资者数量趋势)

本文所展示的代码是一个完整的数据采集、处理与可视化工具,主要用于从指定网站下载Excel文件,解析其中的数据,并生成投资者数量的趋势图表。以下是代码的主要功能模块及其作用:

1.网页数据获取

  • 使用fetch_html_page函数从目标网站抓取HTML页面内容。
  • 通过parse_html_for_excel_links解析HTML内容,提取所有Excel文件链接。
  • 利用parse_html_for_max_page解析最大分页数,确保能够遍历所有页面。

2.文件下载与存储

  • download_excel_file负责根据Excel文件的URL下载文件并保存到本地指定路径。
  • download_excel_data实现批量下载功能,支持多页数据的完整采集。

3.数据读取与处理

  • read_excel_file使用pandas库读取Excel文件内容。
  • process_excel_data将Excel数据转换为字典格式,便于后续处理。
  • process_downloaded_files批量处理下载的Excel文件,提取关键数据并存储为列表。

4.数据可视化

  • plot_investor_trends利用matplotlib绘制双Y轴折线图,展示个人投资者和机构投资者的数量变化趋势。
  • 图表包含日期、个人投资者数量(万名)和机构投资者数量(家),并通过不同颜色区分数据系列。

整体流程

  • 代码从指定网站抓取数据,自动下载相关Excel文件。
  • 解析Excel文件中的投资者数据,并生成趋势图表以直观展示数据变化。
import warningsimport requests
from bs4 import BeautifulSoup
import pandas as pd
import os
import re
import matplotlib# 设置matplotlib的字体配置,以支持中文显示
matplotlib.rcParams['font.sans-serif'] = ['SimHei']  # 或者 ['Microsoft YaHei']
matplotlib.rcParams['axes.unicode_minus'] = False
matplotlib.use('TkAgg')
from matplotlib import pyplot as pltdef fetch_html_page(url):"""获取HTML页面内容。参数:url (str): 目标网页的URL。返回:str: 页面的HTML内容,如果请求失败则返回None。"""headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}response = requests.get(url, headers=headers)if response.status_code == 200:print(f"成功获取页面: {url}")return response.textelse:print(f"Error: {response.status_code}, {response.text}")return Nonedef parse_html_for_excel_links(html_content):"""解析HTML内容中的Excel链接。参数:html_content (str): HTML页面内容。返回:list: 包含所有找到的Excel文件链接的列表。"""soup = BeautifulSoup(html_content, 'html.parser')excel_links = []for a_tag in soup.find_all('a', href=True):href = a_tag.get('href')if href and href.endswith('.xlsx'):excel_links.append(href)return excel_linksdef parse_html_for_max_page(html_content):"""解析HTML内容以找到最大页面数。参数:html_content (str): HTML页面内容。返回:int: 最大页面数。"""soup = BeautifulSoup(html_content, 'html.parser')max_page = 1for a_tag in soup.find_all('a', class_='pagingNormal'):onclick = a_tag.get('onclick')if onclick:match = re.search(r"'(/test/j/[^']+)'", onclick)if match:page_number = match.group(1).split('-')[-1].split('.')[0]max_page = max(max_page, int(page_number))return max_pagedef download_excel_file(url, save_path):"""下载Excel文件并保存到指定路径。参数:url (str): Excel文件的URL。save_path (str): 文件的保存路径。"""headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}response = requests.get(url, headers=headers)if response.status_code == 200:with open(save_path, 'wb') as f:f.write(response.content)print(f"下载完成: {save_path}")else:print(f"Error: {response.status_code}, {response.text}")def download_excel_data():"""下载所有Excel数据文件。"""base_url = 'https://test/index.html'  # 替换为实际网页地址current_url = base_urlpage_number = 1html_content = fetch_html_page(current_url)if not html_content:returnmax_page = parse_html_for_max_page(html_content)print(f"最大页面数: {max_page}")while page_number <= max_page:print(f"正在处理第 {page_number} 页: {current_url}")html_content = fetch_html_page(current_url)if not html_content:breakexcel_links = parse_html_for_excel_links(html_content)if not excel_links:print("未找到Excel链接。")breakfor link in excel_links:full_url = f"https://www.test.cn{link}"# 提取日期和文件名部分file_path_parts = link.split('/')file_name = ('/'.join(file_path_parts[-3:-1]) + '/' + file_path_parts[-1]).replace('/', '-')save_path = os.path.join('downloads', file_name)os.makedirs(os.path.dirname(save_path), exist_ok=True)download_excel_file(full_url, save_path)if page_number < max_page:next_page_link = f"/test/d2bb5c19-{page_number + 1}.html"current_url = f"https://www.test.cn{next_page_link}"page_number += 1else:print("没有更多页面。")breakdef read_excel_file(file_path):"""读取Excel文件内容。参数:file_path (str): Excel文件的路径。返回:DataFrame: 读取到的Excel文件内容,如果读取失败则返回None。"""try:with warnings.catch_warnings():warnings.simplefilter("ignore", UserWarning)df = pd.read_excel(file_path, engine='openpyxl', header=None)return dfexcept Exception as e:print(f"读取Excel文件时出错: {e}")return Nonedef process_excel_data(df):"""处理Excel数据,将其转换为字典格式。参数:df (DataFrame): Excel文件内容。返回:dict: 转换后的字典数据。"""if df is None:return {}# 处理合并单元格# df = df.fillna(method='ffill').fillna(method='bfill')# 将数据转换为字典data_dict = {}current_section = Nonefor index, row in df.iterrows():if index == 1:  # 第二行key = row[1]if pd.isnull(key):key = df.iloc[1, 0]value = row[2] if pd.notnull(row[2]) else Nonedata_dict[key] = valueelif index > 1:if pd.notnull(row[0]):current_section = row[0]data_dict[current_section] = {}if pd.notnull(row[1]):key = row[1]value = row[2] if pd.notnull(row[2]) else Nonedata_dict[current_section][key] = valuereturn data_dictdef process_downloaded_files(directory):"""处理下载的Excel文件,提取数据。参数:directory (str): 存放下载文件的目录路径。返回:list: 包含所有处理后的数据字典的列表。"""data_list = []for filename in os.listdir(directory):if filename.endswith('.xlsx'):file_path = os.path.join(directory, filename)df = read_excel_file(file_path)if df is not None:print(f"处理文件: {filename}")data_dict = process_excel_data(df)print(data_dict)  # 打印处理后的字典data_list.append(data_dict)return data_listdef plot_investor_trends(data_list):"""绘制投资者数量趋势图。参数:data_list (list): 包含投资者数据的列表。"""# 提取时间值和投资者数量dates = []individual_investors = []institutional_investors = []for data_dict in data_list:date_str = data_dict['统计指标']date = pd.to_datetime(date_str)dates.append(date)individual_investors.append(data_dict['证券公司开展业务情况']['个人投资者数量(万名)'])institutional_investors.append(data_dict['证券公司开展业务情况']['机构投资者数量(家)'])# 创建折线图fig, ax1 = plt.subplots(figsize=(10, 6))# 绘制个人投资者数量color = 'tab:red'ax1.set_xlabel('日期')ax1.set_ylabel('个人投资者数量(万名)', color=color)ax1.plot(dates, individual_investors, color=color, label='个人投资者数量(万名)', marker='o')ax1.tick_params(axis='y', labelcolor=color)# 创建第二个 Y 轴ax2 = ax1.twinx()  # 共享 X 轴# 绘制机构投资者数量color = 'tab:blue'ax2.set_ylabel('机构投资者数量(家)', color=color)ax2.plot(dates, institutional_investors, color=color, label='机构投资者数量(家)', marker='o')ax2.tick_params(axis='y', labelcolor=color)# 设置标题和图例fig.tight_layout()  # 调整子图参数,防止标签重叠plt.title('投资者数量趋势')fig.legend(loc='upper left', bbox_to_anchor=(0.1, 0.9))# 显示图形plt.show()# 调用函数绘制投资者趋势图
plot_investor_trends(process_downloaded_files('downloads'))

在这里插入图片描述

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词