From a620e86e052fb24a0d57e4a9e050507d2ba9fc97 Mon Sep 17 00:00:00 2001
From: hnu202111020403
Date: Thu, 30 Dec 2021 18:05:34 +0800
Subject: [PATCH] ADD file via upload
---
(尹杰、张力玺小组).py | 147 ++++++++++++++++++++++++++++++
1 file changed, 147 insertions(+)
create mode 100644 (尹杰、张力玺小组).py
diff --git a/(尹杰、张力玺小组).py b/(尹杰、张力玺小组).py
new file mode 100644
index 0000000..ce74616
--- /dev/null
+++ b/(尹杰、张力玺小组).py
@@ -0,0 +1,147 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Thu Dec 30 15:22:50 2021
+
+@author: 29913
+"""
+
+##计算机与人工智能大作业汇报
+
+# 首先我们需要导入 requests 库
+import requests
+# 请求的url
+url = "https://top.chinaz.com/gongsitop/index_500top.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
+html
+# 导入 re 模快
+import re
+# 使用 findall 函数来获取数据
+# 公司名
+company = re.findall('(.+?)', html)
+# 法定代表人
+person = re.findall('法定代表人:(.+?)
', html)
+# 注册时间
+signDate = re.findall('注册时间:(.+?)', html)
+# 证券类别
+category = re.findall('证券类别:(.+?)', html)
+category
+pageOne = list(zip(company, person, signDate, category))
+pageOne
+# 存储内容
+message = []
+# 总共16个页面的数据
+for page in range(16):
+ # 组装url
+ if page == 0:
+ url = "https://top.chinaz.com/gongsitop/index_500top.html"
+ else:
+ url = "https://top.chinaz.com/gongsitop/index_500top_{}.html".format(page + 1)
+ # 使用reqeusts模快发起 GET 请求
+ response = requests.get(url, headers=headers)
+ html = response.text
+ # 使用 findall 函数来获取数据
+ # 公司名
+ company = re.findall('(.+?)', html)
+ # 法定代表人
+ person = re.findall('法定代表人:(.*?)', html)
+ # 注册时间
+ signDate = re.findall('注册时间:(.*?)', html)
+ # 证券类别
+ category = re.findall('证券类别:(.*?)', html)
+ pageOne = list(zip(company, person, signDate, category))
+ # 合并列表
+ message.extend(pageOne)
+message
+# 导入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", "person", "signDate", "category"])
+df.head()
+df.info()
+
+# 根据证券类型进行分组
+df1 = df.groupby("category").count()["company"]
+df1
+# 在jupyter中直接展示图像
+
+import matplotlib.pyplot as plt
+
+# 用黑体显示中文
+plt.rcParams['font.sans-serif'] = ['SimHei']
+
+# 每个扇形的标签
+labels = df1.index
+# 每个扇形的占比
+sizes = df1.values
+
+fig1, ax1 = plt.subplots()
+# 绘制饼图
+ax1.pie(sizes, labels=labels, autopct='%d%%',radius=2,textprops={'fontsize': 20},
+ shadow=False, startangle=90)
+ax1.axis()
+
+plt.show()
+
+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
+# 导入 re 模快
+import re
+company = re.findall('(.+?)', html)
+
+# 资本
+capital = re.findall('注册资本:(.+?)', html)
+
+pageOne = list(zip(company,capital))
+
+# 存储内容
+message = []
+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", "capital"])
+df.head()
+
+
+df.sort_values(by = "capital",ascending = False)
+df[1:21]
+# 在jupyter中直接展示图像
+%matplotlib inline
+import matplotlib.pyplot as plt
+
+# 用黑体显示中文
+plt.rcParams['font.sans-serif'] = ['SimHei']
+
+x = df['company']
+y = df['capital']
+plt.bar(x,y,color = 'green')
+
+plt.show()