# -*- coding: utf-8 -*- """ Created on Wed Nov 30 12:30:46 2022 @author: lenovo """ 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 # 使用 findall 函数来获取数据 import re # 公司名 company = re.findall('(.+?)', html) # 注册资金: money = re.findall('"CoDate">(.+?)', html) pageOne = list(zip(company,money)) # for循环获取其他页面的所有数据 # 存储内容 message = [] # 所有页面的数据 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('"CoDate">(.+?)', html) pageOne = list(zip(company,money)) # 合并列表 message.extend(pageOne) message=message[0:500] #保存内容到 csv 文件 import csv with open("content.csv", "w") as f: w = csv.writer(f) w.writerows(message) #数据可视化 import pandas as pd import numpy as np df=pd.read_csv("content.csv", names=["company", "money"]) df=df.head(20) df=df[::-1] import matplotlib.pyplot as plt plt.figure() plt.rcParams['font.sans-serif'] = ['SimHei'] x=list(range(1,21)) df_array = np.array(df["company"]) l=df_array.tolist() df_array = np.array(df["money"]) y=df_array.tolist() plt.xticks(x,l,rotation=90) plt.bar(x,y,width=0.8,color='r') plt.xlabel('company') plt.ylabel('money') for i,j in zip(x,y): plt.text(i,j,ha='center',va='bottom') plt.show()