parent
0b08e3da4d
commit
15adb2ced3
@ -0,0 +1,76 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Created on Thu May 12 14:45:14 2022
|
||||||
|
|
||||||
|
@author: pzh
|
||||||
|
"""
|
||||||
|
|
||||||
|
# %% ChinaTop500Company.py
|
||||||
|
import requests
|
||||||
|
import re
|
||||||
|
import csv
|
||||||
|
import pandas as pd
|
||||||
|
# 设置请求头信息
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
# 存储内容
|
||||||
|
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)
|
||||||
|
# 注册资本
|
||||||
|
money = re.findall('注册资本:</span>(.*?)</p>', html)
|
||||||
|
pageOne = list(zip(company, money))
|
||||||
|
# 合并列表
|
||||||
|
message.extend(pageOne)
|
||||||
|
|
||||||
|
with open('content2.csv', 'w') as f:
|
||||||
|
w = csv.writer(f)
|
||||||
|
w.writerows(message)
|
||||||
|
|
||||||
|
# %% 读取数据
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
plt.rcParams['font.sans-serif'] = ['SimHei']
|
||||||
|
plt.rcParams['axes.unicode_minus'] = False
|
||||||
|
df = pd.read_csv("content2.csv", names=["company", "money"], encoding = 'gbk')
|
||||||
|
money = df['money'].values
|
||||||
|
output = []
|
||||||
|
i = 0
|
||||||
|
for elem in money:
|
||||||
|
st = str(elem)
|
||||||
|
if '万美元' in st:
|
||||||
|
num = eval(re.sub(u'([^\u0030-\u0039\u002e\uffe5])', '', st)) / 10000 * 6.79
|
||||||
|
elif '万' in st:
|
||||||
|
num = eval(re.sub(u'([^\u0030-\u0039\u002e\uffe5])', '', st)) / 10000
|
||||||
|
|
||||||
|
elif st == 'nan':
|
||||||
|
num = 0
|
||||||
|
else:
|
||||||
|
num = eval(re.sub(u'([^\u0030-\u0039\u002e\uffe5])', '', st))
|
||||||
|
df['money'].values[i] = round(num, 2)
|
||||||
|
i = i + 1
|
||||||
|
df2 = df.sort_values(by = ['money'], ascending = False)
|
||||||
|
df3 = df2.head(20)
|
||||||
|
|
||||||
|
#绘图
|
||||||
|
x = list(range(20))
|
||||||
|
x = x[::-1]
|
||||||
|
y = df3['money'].values
|
||||||
|
plt.barh(x, y)
|
||||||
|
k = 0
|
||||||
|
plt.yticks(x, df3['company'].values)
|
||||||
|
for i, j in zip(x, y):
|
||||||
|
plt.text(j, i, j, ha = 'right', va = 'center', fontsize = 8)
|
||||||
|
k = k + 1
|
||||||
|
plt.savefig('ChinaTop500Company.png')
|
Loading…
Reference in new issue