diff --git a/pc.py b/pc.py deleted file mode 100644 index 269d22e..0000000 --- a/pc.py +++ /dev/null @@ -1,155 +0,0 @@ -# 中国500强公司信息爬取程序 - - -# 导入所需模块 -import requests # 用于发送HTTP请求获取网页内容 -import re # 用于正则表达式提取数据 -import csv # 用于数据存储为CSV文件 -import pandas as pd # 用于数据处理和分析 -import matplotlib.pyplot as plt # 用于数据可视化 - -# 配置请求URL和请求头 -url = "https://top.chinaz.com/gongsi/index_zhuce.html" # 目标网页URL -headers = { - "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" -} # 设置请求头,模拟浏览器访问 - -# 发送GET请求获取网页内容 -response = requests.get(url, headers=headers) -html = response.text # 获取响应的HTML文本 - -# 提取第一页公司基础信息(公司名、法定代表人、注册时间、证券类别) -company = re.findall('(.+?)', html) # 提取公司名称 -person = re.findall('法定代表人:(.+?)<', html) # 提取法定代表人 -signDate = re.findall('注册时间:(.+?)<', html) # 提取注册时间 -category = re.findall('证券类别:(.+?)<', html) # 提取证券类别 - -# 合并第一页数据 -pageOne = list(zip(company, person, signDate, category)) -print("第一页数据示例:", pageOne[:2]) # 打印前两条数据示例 - -# 存储所有页面数据的列表 -message = [] - -# 循环爬取16个页面的数据 -for page in range(16): - # 组装不同页面的URL - if page == 0: - current_url = "https://top.chinaz.com/gongsi/index_zhuce.html" - else: - current_url = f"https://top.chinaz.com/gongsi/index_zhuce_{page + 1}.html" # 修正URL格式 - - # 发送请求获取当前页面内容 - response = requests.get(current_url, headers=headers) - html = response.text - - # 提取当前页面的公司基础信息 - company = re.findall('(.+?)', html) - person = re.findall('法定代表人:(.+?)<', html) - signDate = re.findall('注册时间:(.+?)<', html) - category = re.findall('证券类别:(.+?)<', html) - - # 合并当前页面数据并添加到总数据列表 - page_data = list(zip(company, person, signDate, category)) - message.extend(page_data) - -# 将数据保存到CSV文件 -with open("content.csv", "w", newline='', encoding='utf-8-sig') as f: - writer = csv.writer(f) - writer.writerows(message) -print("基础数据已保存至content.csv") - -# 使用pandas读取CSV数据 -df = pd.read_csv("content.csv", names=["company", "person", "signDate", "category"]) -print("数据前5行示例:") -print(df.head()) # 打印数据前5行 -print("数据基本信息:") -df.info() # 打印数据基本信息 - -# 按证券类别分组统计公司数量 -df1 = df.groupby("category").count()["company"] -print("按证券类别分组统计结果:") -print(df1) - -# 定义函数用于提取公司名称和注册资本 -def get_company_data(url, headers): - response = requests.get(url, headers=headers) - html = response.text - - # 提取公司名称(改进正则表达式,匹配完整标签) - company = re.findall('(.+?)', html) - - # 提取注册资本信息 - capital = re.findall('注册资本:(.+?)

', html) - - # 处理注册资本,转换为数值(单位:万元) - capital_values = [] - for cap in capital: - try: - # 提取数字部分,处理带逗号的格式如"1,000.5万" - num_part = re.search(r'(\d+(?:,\d+)*(?:\.\d+)?)', cap) - if num_part: - num = float(num_part.group(1).replace(',', '')) - # 单位转换:如果包含"亿",转换为万元 - if '亿' in cap: - num *= 10000 - capital_values.append(num) - else: - capital_values.append(0) - except: - capital_values.append(0) - - return list(zip(company, capital_values)) - -# 爬取所有页面的注册资本数据 -all_company_data = [] -for page in range(16): - if page == 0: - current_url = "https://top.chinaz.com/gongsi/index_zhuce.html" - else: - current_url = f"https://top.chinaz.com/gongsi/index_zhuce_{page + 1}.html" - - print(f"正在爬取第 {page + 1} 页数据...") - page_data = get_company_data(current_url, headers) - all_company_data.extend(page_data) - -# 将注册资本数据转换为DataFrame -df_capital = pd.DataFrame(all_company_data, columns=['company', 'capital']) - -# 按注册资本降序排序,取前20名 -top20 = df_capital.sort_values('capital', ascending=False).head(20) - -# 打印前20名公司的注册资本信息 -print("\n注册资金最多的20家公司:") -print(top20) - -# 绘制前20名公司注册资本的柱状图 -plt.figure(figsize=(14, 8)) # 设置图表大小 -bars = plt.bar(top20['company'], top20['capital'], color='#4CAF50') # 绘制柱状图 - -# 在每个柱子上方添加具体数值 -for bar in bars: - height = bar.get_height() - plt.text(bar.get_x() + bar.get_width()/2., height, - f'{height:.1f}万元', ha='center', va='bottom', fontsize=10) - -# 设置图表标题和坐标轴标签 -plt.title('中国500强公司中注册资金最多的前20家公司', fontsize=16) -plt.xlabel('公司名称', fontsize=14) -plt.ylabel('注册资金(万元)', fontsize=14) - -# 旋转x轴标签以便更好显示 -plt.xticks(rotation=45, ha="right", fontsize=12) - -# 添加网格线 -plt.grid(axis="y", linestyle="--", alpha=0.7) - -# 调整布局 -plt.tight_layout() - -# 显示图表 -plt.show() - -# 保存前20名公司数据到CSV文件 -top20.to_csv('top20_company_capital.csv', index=False, encoding='utf-8-sig') -print("前20名公司数据已保存到 top20_company_capital.csv") \ No newline at end of file