Update 中国500强公司信息爬取.py

master
hnu202110040421 3 years ago
parent b5fd209ee3
commit d8e84cb23a

@ -1,30 +1,30 @@
import requests import requests
# 设置请求头信息 # 设置请求头信息
headers = { headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47" "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.53"
} }
import re import re
# 存储内容 # 存储内容
message = [] message = []
# 总共16个页面的数据 # 总共17个页面的数据
for page in range(16): for page in range(17):
# 组装url # 请求的url # 组装url # 请求的url
if page == 0: if page == 0:
# https://top.chinaz.com/gongsitop/index_500top.html # https://top.chinaz.com/gongsi/index_zhuce.html
url = "https://top.chinaz.com/gongsitop/index_500top.html" url = "https://top.chinaz.com/gongsi/index_zhuce.html"
else: else:
# https://top.chinaz.com/gongsitop/index_500top_2.html # https://top.chinaz.com/gongsi/index_zhuce_2.html
url = "https://top.chinaz.com/gongsitop/index_500top_{}.html".format(page + 1) url = "https://top.chinaz.com/gongsi/index_zhuce_{}.html".format(page + 1)
# 使用reqeusts模快发起 GET 请求 # 使用reqeusts模快发起 GET 请求
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
# 获取请求的返回结果 # 获取请求的返回结果
html = response.text html = response.text
# 使用 findall 函数来获取数据 # 使用 findall 函数来获取数据
# 公司名 # 公司名
# <a href="/company/ZhongGuoShiYouHuaGongGuFen.html" target="_blank">中国石油化工股份有限公司</a></h3> # <a href="/company/ZhongGuoTieLu.html" target="_blank">中国铁路总公司</a></h3>
company = re.findall('<a.*?target="_blank">(.+?)</a></h3>', html) company = re.findall('<a.*?target="_blank">(.+?)</a></h3>', html)
# 注册资本 # 注册资本
# 注册资本:</span>1210.71亿元</p> # 注册资本:</span>103600000万人民币</p>
money = re.findall('注册资本:</span>(.*?)</p>', html) money = re.findall('注册资本:</span>(.*?)</p>', html)
# 对应项目的信息进行打包 # 对应项目的信息进行打包
pageOne = list(zip(company, money)) pageOne = list(zip(company, money))
@ -40,10 +40,10 @@ df = pd.read_csv("content01.csv", names=["company", "money"], encoding='gbk')
# 填充空元素 # 填充空元素
df = df.fillna('0') df = df.fillna('0')
# 注册资本单位转换 # 注册资本单位转换
company_all=list(df['company']) company_all_top500=list(df.loc[0:499,'company'])
money_all=list(df['money']) money_all_top500=list(df.loc[0:499,'money'])
money_all_number=[] money_all_number=[]
for i in money_all: for i in money_all_top500:
p='' p=''
for j in i: for j in i:
if j in '0123456789.': if j in '0123456789.':
@ -59,7 +59,7 @@ for i in money_all:
if '' in i: if '' in i:
p=int(p*0.8585+0.5) p=int(p*0.8585+0.5)
money_all_number.append(p) money_all_number.append(p)
data={'company':company_all,'money':money_all_number} data={'company':company_all_top500,'money':money_all_number}
df=pd.DataFrame(data) df=pd.DataFrame(data)
df=df.sort_values(by=['money'],ascending=False) df=df.sort_values(by=['money'],ascending=False)
# 取注册资本最多的前二十公司名称和注册资本 # 取注册资本最多的前二十公司名称和注册资本
@ -87,7 +87,7 @@ plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False plt.rcParams['axes.unicode_minus'] = False
plt.bar(range(20),money_top20) plt.bar(range(20),money_top20)
plt.xticks(range(20),company_top20_y) plt.xticks(range(20),company_top20_y)
plt.yticks([5e10,10e10,15e10,20e10,25e10,30e10,35e10],['500亿','1000亿','1500亿','2000亿','2500亿','3000亿','3500亿']) plt.yticks([2000e8,4000e8,6000e8,8000e8,10000e8],['2000','4000','6000','8000','10000'])
plt.ylabel('注册资') plt.ylabel('注册资本/亿元')
plt.title('中国500强公司注册资金前二十的公司及注册资金示意图') plt.title('中国500强公司注册资金前二十的公司及注册资金示意图')
plt.show() plt.show()
Loading…
Cancel
Save