From 7e8685b2501692f13431f125d580772156ab7e44 Mon Sep 17 00:00:00 2001 From: hnu202109070101 Date: Tue, 31 May 2022 21:15:53 +0800 Subject: [PATCH] =?UTF-8?q?Delete=20'=E4=B8=AD=E5=9B=BD=E4=BA=94=E7=99=BE?= =?UTF-8?q?=E5=BC=BA=E5=85=AC=E5=8F=B8=E4=BF=A1=E6=81=AF=E7=88=AC=E5=8F=96?= =?UTF-8?q?.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 中国五百强公司信息爬取.py | 100 --------------------------- 1 file changed, 100 deletions(-) delete mode 100644 中国五百强公司信息爬取.py diff --git a/中国五百强公司信息爬取.py b/中国五百强公司信息爬取.py deleted file mode 100644 index 9ab162e..0000000 --- a/中国五百强公司信息爬取.py +++ /dev/null @@ -1,100 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Created on Sat May 21 21:38:27 2022 - -@author: dell -""" - -# 首先我们需要导入 requests 库 -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 -print(html) -# 导入 re 模快 -import re -# 使用 findall 函数来获取数据 -# 公司名 -company = re.findall('(.+?)', html) - -#注册资本 -money=re.findall('(.+?)注册资本', html) - -pageOne = list(zip(company,money)) - - -# 存储内容 -message = [] -# 总共16个页面的数据 -for page in range(17): - # 组装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) - - - -# 导入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",'money'],encoding='gbk') -df.head() -df.info() - -#数据预处理 -df['money']=df['money'].apply(lambda x:x.replace('亿','') ) -df['money']=df['money'].apply(lambda x: float(x) ) -#获取top20 -df1=df.sort_values(by='money',ascending=False) -df2=df1.iloc[0:20] -#数据可视化 绘制水平条形图 -import matplotlib.pyplot as plt -x=df2['company'] -y=df2['money'] -plt.barh(x,y,height=0.8) -for i,j in zip(x,y): - plt.text(j,i,j) -plt.title('中国500强注册资金top20企业') -plt.show() - - - - - - - - - - - - - - - -