diff --git a/chinaz(1).py b/chinaz(1).py new file mode 100644 index 0000000..75242cd --- /dev/null +++ b/chinaz(1).py @@ -0,0 +1,39 @@ +import csv +import requests +import re +import pandas as pd +import matplotlib.pyplot as plt +page_num = 17 +message = [] +for i in range(1, page_num + 1): + if i == 1: + url = 'https://top.chinaz.com/gongsi/index_zhuce.html' + else: + url = f'https://top.chinaz.com/gongsi/index_zhuce_{i}.html' + headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46" + } + resp = requests.get(url,headers=headers) + html = resp.text + company = re.findall('(.+?)', html) + register_money = re.findall('
(.+?)亿注册资本
', html) + pageOne = list(zip(company, register_money)) + message.extend(pageOne) + print(f"第{i}页爬取完毕") +with open("content.csv", "w", encoding='utf-8', newline='') as f: + w = csv.writer(f) + w.writerows(message) +plt.rcParams['font.sans-serif'] = ['SimHei'] +df = pd.read_csv("content.csv", names=["company","register_money"]) +df.sort_values(by=['register_money']) +xlabels = df["company"].values[:20] +ylabels = df["register_money"].values[:20] +data = pd.Series(ylabels, index=xlabels) +data.plot(kind='bar') +plt.title('注册资金最多的公司 top20') +plt.xlabel('公司') +plt.xticks(rotation=270,fontsize=5) +plt.ylabel('注册资金') +for x, y in enumerate(data): + plt.text(x, y, y) +plt.show() \ No newline at end of file