From 464d3721244b8e61850963881fe3778e4d1eba8a Mon Sep 17 00:00:00 2001 From: hnu202401010411 <1135844023@qq.com> Date: Tue, 3 Dec 2024 21:01:49 +0800 Subject: [PATCH] ADD file via upload --- replite.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 replite.py diff --git a/replite.py b/replite.py new file mode 100644 index 0000000..3ea1c1b --- /dev/null +++ b/replite.py @@ -0,0 +1,68 @@ +# 首先我们需要导入 requests 库 +import requests +# 若是本地没有安装 requests 可以通过“pip install requests”来安装 + +# 设置请求头信息 +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" +} + +# 导入 re 模快 +import re + +# 存储内容 +message = [] +# 总共16个页面的数据 +for page in range(20): + # 组装url + if page == 0: + url = "https://top.chinaz.com/gongsi/index_zhuce.html" + else: + url = "https://top.chinaz.com/gongsi/index_zhuce_{}.html".format(page + 1) + # 使用reqeusts模快发起 GET 请求 + response = requests.get(url, headers=headers) + html = response.text + # 使用 findall 函数来获取数据 + # 公司名 + company = re.findall('(.+?)', html) + # 注册资金 + signMoney = re.findall('
(.+?)注册资本
', html) + + pageOne = list(zip(company,signMoney)) + # 合并列表 + message.extend(pageOne) + +# 导入python中的内置模块csv +import csv +with open("content.csv", "w") as f: + w = csv.writer(f) + w.writerows(message) + +import pandas as pd + +# 读取数据 +df = pd.read_csv("content.csv", names=["company", "signMoney"],encoding='gbk') + +df1 = df.drop(0).head(20) + +import matplotlib.pyplot as plt + +# 在spyter中直接展示图像 +%matplotlib inline +# 设置中文显示 +plt.rcParams['font.sans-serif'] = ['SimHei'] +plt.rcParams['figure.figsize'] = (30, 5) # 设置figure_size尺寸 + +# x轴坐标 +x = df1["company"].values +# y轴坐标 +y = df1["signMoney"].values +# 绘制柱状图 +plt.bar(x, y) +# 设置x轴名称 +plt.xlabel("公司",fontsize=14) +# 设置x轴名称 +plt.ylabel("注册资本",fontsize=14) +plt.show() + +