diff --git a/1.py b/1.py new file mode 100644 index 0000000..a1e8788 --- /dev/null +++ b/1.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +""" +Created on Sat Jun 1 19:31:04 2024 + +@author: Asus +""" + +import requests +# 请求的url +url = "https://top.chinaz.com/gongsi/index_zhuce.html" +# 设置请求头信息 +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" +} +# 使用reqeusts模快发起 GET 请求 +response = requests.get(url, headers=headers) +# 获取请求的返回结果 +html = response.text +import re +# 公司名 +company = re.findall('(.+?)', html) +# 注册资本 +money = re.findall('注册资本:(.+?)

', html) +pageOne = list(zip(company, money)) +# 存储内容 +message = [] +# 总共16个页面的数据 +for page in range(16): + # 组装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) + # 注册资本 + money = re.findall('注册资本:(.+?)

', html) + pageOne = list(zip(company, money)) + # 合并列表 + message.extend(pageOne) +print(message) +import csv +with open("content.csv", "w",encoding="utf-8") as f: + w = csv.writer(f) + w.writerows(message) + +import pandas as pd + +# 读取数据 +df = pd.read_csv("content.csv", names=["company", "money"]) +df.head() +df.info() +print(df) +df1 = df.groupby("money").count()["company"] +top20=df.sort_values(by='money',ascending=False).head(20) + +import matplotlib.pyplot as plt +plt.figure(figsize=(10, 6)) # 设置图像大小 +plt.bar(top20['company'], top20['money'], color='skyblue') # 绘制条形图 +plt.title('注册资金最多的公司TOP20') # 设置标题 +plt.xlabel('公司名称') # 设置x轴标签 +plt.ylabel('注册资本') # 设置y轴标签 +plt.xticks(rotation=45) # 设置x轴标签旋转角度,以便更好地显示 +plt.tight_layout() # 调整布局,避免标签重叠 +plt.show() # 显示图像 \ No newline at end of file