You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.0 KiB
72 lines
2.0 KiB
#首先我们需要导入 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
|
|
# 导入 re 模快
|
|
import re
|
|
# 存储内容
|
|
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('<a.*?target="_blank">(.+?)</a></h3>', html)
|
|
#注册资本
|
|
registered=re.findall('注册资本:</span>(.*?)</p>',html)
|
|
pageOne = list(zip(company,registered))
|
|
# 合并列表
|
|
message.extend(pageOne)
|
|
# 导入python中的内置模块csv
|
|
import csv
|
|
with open("content.csv", "w") as f:
|
|
w = csv.writer(f)
|
|
w.writerows(message)
|
|
!cat content.csv
|
|
import pandas as pd
|
|
|
|
# 读取数据
|
|
df = pd.read_csv("content.csv", names=["company", "registered"])
|
|
df.head()
|
|
df.info()
|
|
# 在jupyter中直接展示图像
|
|
%matplotlib inline
|
|
import matplotlib.pyplot as plt
|
|
|
|
# 用黑体显示中文
|
|
plt.rcParams['font.sans-serif'] = ['SimHei']
|
|
|
|
#
|
|
df['registered']=df['registered'].astype(str)
|
|
df['registered'] = df['registered'].str.extract('(\d+(?:\.\d+)?)',expand=False).astype(float)
|
|
|
|
#降序
|
|
df_sorted=df.sort_values(by='registered',ascending=False)
|
|
|
|
#提取前20位
|
|
top_20=df_sorted.head(20)
|
|
|
|
#柱状图
|
|
plt.bar(top_20['company'],top_20['registered'])
|
|
plt.xticks(rotation=45)
|
|
plt.xlabel('公司名字')
|
|
plt.ylabel('注册资本')
|
|
|
|
#
|
|
plt.show
|